Strange thing in Rails views

39 views
Skip to first unread message

Karthikeyan A K

unread,
Apr 1, 2014, 1:11:51 AM4/1/14
to rubyonra...@googlegroups.com
Hello people,

I am developing an app with Rails 4.0.x and I have a strange bug in its view. Say I have a partial like this

%p= @bject,name # would work perfectly

= render @object.friends # It won't fetch this relation

%p= @object,name # It would say can't find name for nil class

For some reason the above stuff happens. However in the rails console, all would work fine. Kinda confused.

Would be great if any one could throw light on this. Don' know if this is a Rails bug where state of instance variable gets forgotten after some time.


-----

Regards

Karthikeyan A K

Colin Law

unread,
Apr 1, 2014, 4:34:38 AM4/1/14
to rubyonra...@googlegroups.com
On 1 April 2014 06:11, Karthikeyan A K <77m...@gmail.com> wrote:
> Hello people,
>
> I am developing an app with Rails 4.0.x and I have a strange bug in its
> view. Say I have a partial like this
>
> %p= @bject,name # would work perfectly

Does using @object,name also work (not @bject which you have written above)?

>
> = render @object.friends # It won't fetch this relation

What do you mean it will not fetch the relation? What is the error message?

>
> %p= @object,name # It would say can't find name for nil class

Then @object is nil. Perhaps you should be using @bject.

Note that when posting code it is better to copy/paste then there will
be no confusion about what the code actually is.

>
> For some reason the above stuff happens. However in the rails console, all
> would work fine. Kinda confused.
>
> Would be great if any one could throw light on this. Don' know if this is a
> Rails bug where state of instance variable gets forgotten after some time.

My experience is that bugs in compilers/frameworks/OSs are very rarely
the cause of problems. 99.999% it is one's own code that is at fault.

Colin

Karthikeyan A K

unread,
Apr 1, 2014, 6:50:20 AM4/1/14
to rubyonra...@googlegroups.com
On Tue, Apr 1, 2014 at 2:04 PM, Colin Law <cla...@gmail.com> wrote:
On 1 April 2014 06:11, Karthikeyan A K <77m...@gmail.com> wrote:
> Hello people,
>
> I am developing an app with Rails 4.0.x and I have a strange bug in its
> view. Say I have a partial like this
>
> %p= @bject,name # would work perfectly

Does using @object,name also work (not @bject which you have written above)?

Actually the variable name is not @Object. Its some thing else.  Have checked spelling, copied and pasted it. But nope. It won't work.


>
> = render @object.friends # It won't fetch this relation

What do you mean it will not fetch the relation?  What is the error message?
 
There shows no error, it just returns nil object. If I give @obj.friends.first.name , it would say undefined method first for nil object!

>
> %p= @object,name # It would say can't find name for nil class

Then @object is nil.  Perhaps you should be using @bject.

Note that when posting code it is better to copy/paste then there will
be no confusion about what the code actually is.

Yup copied, pasted and banged my head on desk a 1000 times.

Say if in the action if I keep @object2 = Person.find params[:id] and if I use @object2 in code, it works perfectly everywhere. This bug is repeatable on server and my local system running ruby 2.1.0 and Rails 4.3.

Some how rails is forgetting what @object is in the middle of view.
>
> For some reason the above stuff happens. However in the rails console, all
> would work fine. Kinda confused.
>
> Would be great if any one could throw light on this. Don' know if this is a
> Rails bug where state of instance variable gets forgotten after some time.

My experience is that bugs in compilers/frameworks/OSs are very rarely
the cause of problems.  99.999% it is one's own code that is at fault.

Colin

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/UaWUMHoGo90/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtqbxQr719tqtxd_CO6%2BSPRDJRt-KzVooZRL33UsaR%3D-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Apr 1, 2014, 8:36:32 AM4/1/14
to rubyonra...@googlegroups.com
On 1 April 2014 11:50, Karthikeyan A K <77m...@gmail.com> wrote:
>
>
> On Tue, Apr 1, 2014 at 2:04 PM, Colin Law <cla...@gmail.com> wrote:
>>
>> On 1 April 2014 06:11, Karthikeyan A K <77m...@gmail.com> wrote:
>> > Hello people,
>> >
>> > I am developing an app with Rails 4.0.x and I have a strange bug in its
>> > view. Say I have a partial like this
>> >
>> > %p= @bject,name # would work perfectly
>>
>> Does using @object,name also work (not @bject which you have written
>> above)?
>
>
> Actually the variable name is not @Object. Its some thing else. Have
> checked spelling, copied and pasted it. But nope. It won't work.
>
>>
>> >
>> > = render @object.friends # It won't fetch this relation
>>
>> What do you mean it will not fetch the relation? What is the error
>> message?
>
>
> There shows no error, it just returns nil object. If I give
> @obj.friends.first.name , it would say undefined method first for nil
> object!
>>
>>
>> >
>> > %p= @object,name # It would say can't find name for nil class
>>
>> Then @object is nil. Perhaps you should be using @bject.
>>
>> Note that when posting code it is better to copy/paste then there will
>> be no confusion about what the code actually is.
>
>
> Yup copied, pasted and banged my head on desk a 1000 times.

I meant that you should copy/paste the code here rather than typing
something similar for us to look at.

>
> Say if in the action if I keep @object2 = Person.find params[:id] and if I
> use @object2 in code, it works perfectly everywhere. This bug is repeatable
> on server and my local system running ruby 2.1.0 and Rails 4.3.
>
> Some how rails is forgetting what @object is in the middle of view.

I don't think it is at all likely that it is forgetting it, but if you
don't show us the actual failing code then it is difficult to help.
You could try inserting puts statements in the code (which will appear
in the server log) between the point at which it knows the variable
and the point at which you think it has forgotten, in order to see
which bit of code is causing it to be 'forgotten'.

As I said it is most likely something in your code that is causing the
problem, but if you do not show us the actual code (just the failing
area) then it is difficult to help.

Colin

Matt Jones

unread,
Apr 1, 2014, 2:49:11 PM4/1/14
to rubyonra...@googlegroups.com


On Tuesday, 1 April 2014 06:50:20 UTC-4, Karthikeyan A K wrote:


On Tue, Apr 1, 2014 at 2:04 PM, Colin Law <cla...@gmail.com> wrote:
On 1 April 2014 06:11, Karthikeyan A K <77m...@gmail.com> wrote:
> Hello people,
>
> I am developing an app with Rails 4.0.x and I have a strange bug in its
> view. Say I have a partial like this
>
> %p= @bject,name # would work perfectly

Does using @object,name also work (not @bject which you have written above)?

Actually the variable name is not @Object. Its some thing else.  Have checked spelling, copied and pasted it. But nope. It won't work.


>
> = render @object.friends # It won't fetch this relation

What do you mean it will not fetch the relation?  What is the error message?
 
There shows no error, it just returns nil object. If I give @obj.friends.first.name , it would say undefined method first for nil object!

>
> %p= @object,name # It would say can't find name for nil class

Then @object is nil.  Perhaps you should be using @bject.

Note that when posting code it is better to copy/paste then there will
be no confusion about what the code actually is.

Yup copied, pasted and banged my head on desk a 1000 times.

Say if in the action if I keep @object2 = Person.find params[:id] and if I use @object2 in code, it works perfectly everywhere. This bug is repeatable on server and my local system running ruby 2.1.0 and Rails 4.3.

Some how rails is forgetting what @object is in the middle of view.


I'm going to assume the "3" in "4.3" is a typo - otherwise, say hi to the future people for me. :)

Anyways, the usual cause of weird name-specific behavior is that you've picked a name which is used internally by part of Rails. Sanitizing your example to "@object" rather than the real name is going to make this difficult to troubleshoot...

--Matt Jones 

Karthikeyan A K

unread,
Apr 2, 2014, 12:52:36 AM4/2/14
to rubyonra...@googlegroups.com
Ya possibly. Any way did a work around. Thanks a lot for your replies.


--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/UaWUMHoGo90/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages