Stuck on passing variable from controller to view

Hello,

I am pretty new to RoR and I am stuck on this problem and not even sure
how to troubleshoot it. The issue is that my index.rhtml is not
displaying any variables from my controller.

Controller (versions_controller.rb)

class VersionController < ApplicationController
def index
mydate = “20090521”
end

View (index.rhtml)

<%= @mydate %> 1

For some reason the view doesn’t see the variable. I also have a
def query



end

in the same controller and it works fine (feeding query.rhtml without
any issues).

I can even put garbage in the controller such as “a;lskjdfj” and it
doesn’t give me a compile error in the controller.

Any help would be appreciated.

Thanks,

Marek

Marek N. wrote:

Hello,

I am pretty new to RoR and I am stuck on this problem and not even sure
how to troubleshoot it. The issue is that my index.rhtml is not
displaying any variables from my controller.

Controller (versions_controller.rb)

class VersionController < ApplicationController
def index
mydate = “20090521”
end

That should be @mydate.

View (index.rhtml)

<%= @mydate %> 1

[…]

Thanks,

Marek

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi,

Try this :
*
class VersionController < ApplicationController
def index
@mydate = “20090521”
end*

On Fri, May 22, 2009 at 2:02 PM, Marek N.

Rui Carvalho wrote:

Hi,

Try this :
*
class VersionController < ApplicationController
def index
@mydate = “20090521”
end*

On Fri, May 22, 2009 at 2:02 PM, Marek N.

Hi, thank you both for the quick reply. I did try that - but it still
does not pass the value to the view… Is there something outside of
the controller and view that could affect this?

Thanks,
Mark

Hi,

Try this :*
@mydate = “20090521”
*

On Fri, May 22, 2009 at 2:02 PM, Marek N.

Rui Carvalho wrote:

Hi,

Try this :*
@mydate = “20090521”
*

On Fri, May 22, 2009 at 2:02 PM, Marek N.

Hello,

Do you mean to add the “@” symbol? I had tried that and numerous other
things and it did not work.

Also, how could I troubleshoot this? If I could see why the .rhtml is
ignoring part of the .rb file, it would help…

Thanks,
Marek

To check that the view is interpreting this you could try <%= “value is
#{@mydate}” %>. If you see the fixed text then it is just that @mydate
is
not getting setup for some reason. Possibly a typo that you just can’t
get
your eye on.

What do you mean by “I can even put garbage in the controller such as
“a;lskjdfj” and it
doesn’t give me a compile error in the controller.”

Colin

2009/5/22 Marek N. [email protected]

Post the contents of versions_controller.rb, there must be something
wrong
there. Strip out some of the unneeded stuff if it is more than a few
tens of
lines. If you do change it, make sure that it still fails, and that
query
still works.
Colin

2009/5/22 Marek N. [email protected]

Colin L. wrote:

To check that the view is interpreting this you could try <%= “value is
#{@mydate}” %>. If you see the fixed text then it is just that @mydate
is
not getting setup for some reason. Possibly a typo that you just can’t
get
your eye on.

What do you mean by “I can even put garbage in the controller such as
“a;lskjdfj” and it
doesn’t give me a compile error in the controller.”

Colin

2009/5/22 Marek N. [email protected]

Hello Colin,

That is a good idea for a test case and the fixed text does appear, but
not the variable’s value. What kind of typo could cause this? The odd
thing is that I have a “def query” below it that is able to pass values
to “query.rhtml” without any issues…

As for your question, I have two rhtml files, index and query. If I put
meaningless junk in “def query” I get the following interpretation error
(as expected):

undefined local variable or method `laksjdf' for 

#VersionController:0xf6993310

if I copy paste the same junk text into “def index”, it does not produce
an error (odd right?). This seems to indicate that
version_controller.rb is not interpreting the code in “def index” for
some reason, but is interpreting it in the “def query” which is directly
below it. This is at least consistent with why it is not showing the
value for @mydate. I don’t know enough about ruby to figure out what
could cause this sort of behaviour.

Thanks,
Mark

Colin L. wrote:

The class should be VersionsController, not VersionController, though
that
may just be a typo in your post.

2009/5/22 Colin L. [email protected]

Yes, that was a typo on my part. The Controller and .rb file are both
singular.

Thanks,
Marek

They should both be plural, versions_controller.rb and
VersionsController
class

2009/5/22 Marek N. [email protected]

Colin L. wrote:

They should both be plural, versions_controller.rb and
VersionsController
class

2009/5/22 Marek N. [email protected]

Is this causing the issue?

Thanks,
Marek

I don’t know, life will be easier if you stick to the conventions though
it
may not be the direct cause of the problem

The convention would be:
table versions
model version.rb, class Version
controller versions_controller.rb class VersionsController
views directory versions

Colin

2009/5/22 Marek N. [email protected]

On Fri, May 22, 2009 at 9:04 AM, Colin L. [email protected]
wrote:

Is this causing the issue?

Thanks,
Marek

Posted via http://www.ruby-forum.com/.

In addition to the previous responders to your post, I would recommend
reading the following:

Good luck,

-Conrad

Conrad T. wrote:

On Fri, May 22, 2009 at 9:04 AM, Colin L. [email protected]
wrote:

Is this causing the issue?

Thanks,
Marek

Posted via http://www.ruby-forum.com/.

In addition to the previous responders to your post, I would recommend
reading the following:

Getting Started with Rails — Ruby on Rails Guides

Good luck,

-Conrad

Thank you Colin/Conrad, I appreciate your assistance. I am going to
read through that doc again (didn’t get everything the first time) and
see if I can see what is going on. I’ll post the solution if I find one
without starting over and renaming the project. Enjoy the weekend!

Best regards,
Marek

The class should be VersionsController, not VersionController, though
that
may just be a typo in your post.

2009/5/22 Colin L. [email protected]