Forum: Ruby on Rails Stuck on passing variable from controller to view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 15:02
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)
==================
<table>
<td>
<%= @mydate %>
1
</td></tr>
</table>

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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-22 15:12
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)
> ==================
> <table>
> <td>
> <%= @mydate %>
> 1
> </td></tr>
> </table>

[...]
>
> Thanks,
>
> Marek

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
9948ccb3aabc47f0f3e8c8b439a9d831?d=identicon&s=25 Rui Carvalho (Guest)
on 2009-05-22 15:13
(Received via mailing list)
Hi,

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




On Fri, May 22, 2009 at 2:02 PM, Marek N.
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 15:22
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
9948ccb3aabc47f0f3e8c8b439a9d831?d=identicon&s=25 Rui Carvalho (Guest)
on 2009-05-22 16:13
(Received via mailing list)
Hi,

Try this :*
  @mydate = "20090521"
*


On Fri, May 22, 2009 at 2:02 PM, Marek N.
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 16:19
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-22 17:00
(Received via mailing list)
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. <rails-mailing-list@andreas-s.net>
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 17:21
Colin Law 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. <rails-mailing-list@andreas-s.net>

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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-22 17:28
(Received via mailing list)
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. <rails-mailing-list@andreas-s.net>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-22 17:32
(Received via mailing list)
The class should be VersionsController, not VersionController, though
that
may just be a typo in your post.

2009/5/22 Colin Law <clanlaw@googlemail.com>
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 17:35
Colin Law wrote:
> The class should be VersionsController, not VersionController, though
> that
> may just be a typo in your post.
>
> 2009/5/22 Colin Law <clanlaw@googlemail.com>

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

Thanks,
Marek
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-22 17:38
(Received via mailing list)
They should both be plural, versions_controller.rb and
VersionsController
class

2009/5/22 Marek N. <rails-mailing-list@andreas-s.net>
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 17:56
Colin Law wrote:
> They should both be plural, versions_controller.rb and
> VersionsController
> class
>
> 2009/5/22 Marek N. <rails-mailing-list@andreas-s.net>

Is this causing the issue?

Thanks,
Marek
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-22 18:06
(Received via mailing list)
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. <rails-mailing-list@andreas-s.net>
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-05-22 18:16
(Received via mailing list)
On Fri, May 22, 2009 at 9:04 AM, Colin Law <clanlaw@googlemail.com>
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:

http://guides.rubyonrails.org/getting_started.html

Good luck,

-Conrad
Eb8eeb55128a2a4ff8bd001c06f5e9c9?d=identicon&s=25 Marek N. (-marek-)
on 2009-05-22 21:30
Conrad Taylor wrote:
> On Fri, May 22, 2009 at 9:04 AM, Colin Law <clanlaw@googlemail.com>
> 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:
>
> http://guides.rubyonrails.org/getting_started.html
>
> 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
This topic is locked and can not be replied to.