Jason Frankovitz
2007-Jun-28 19:01 UTC
complete stumper: variable exists in hidden_field_tag, but nil everywhere else
This is a real mystery to me. I have an instance variable in a partial
that only gives its value inside a hidden_field_tag. If I try to use
the variable anywhere else in the partial, i either get nil or an
undefined error. The goal is to use the value of @event.id outside of
hidden_field_tag.
Here''s what works:
<%= hidden_field_tag ''id'', @event && @event.id, :id
=> ''event_id'' %>
...spits out this HTML: <input type="hidden" name="id"
id="event_id"
value="771"/>
So I read that to mean that @event.id is 771. Fine, good. However, if
I change the line to this:
<%= hidden_field_tag ''id'', @event.id, :id =>
''event_id'' %>
...I get an error:
RuntimeError in Controller#index
Showing app/views/controller/_contacts.rhtml where line #81 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id
Furthermore, if I say this elsewhere in the same partial:
the event id is: <%= @event && @event.id %>END
...I get this:
the event is: END
And finally if I do this:
<%= hidden_field_tag ''id'', @event &&
@event.id, :id =>
''event_id'' %>
<%= hidden_field_tag ''id'', @event &&
@event.id, :id =>
''event_id'' %>
<%= hidden_field_tag ''id'', @event &&
@event.id, :id =>
''event_id'' %>
<%= hidden_field_tag ''id'', @event &&
@event.id, :id =>
''event_id'' %>
<%= hidden_field_tag ''id'', @event &&
@event.id, :id =>
''event_id'' %>
...only the FIRST one works:
<input id="event_id" type="hidden" name="id"
value="771"/>
<input id="event_id" type="hidden"
name="id"/>
<input id="event_id" type="hidden"
name="id"/>
<input id="event_id" type="hidden"
name="id"/>
<input id="event_id" type="hidden"
name="id"/>
Anyone got a handle on this? I''ve never seen a problem like this
before and it''s driving me completely nuts. I''d really be
grateful for
any tips or fixes.
Thanks!
-Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Jun-28 20:10 UTC
Re: complete stumper: variable exists in hidden_field_tag, but nil everywhere else
Hi Jason, Jason Frankovitz wrote:> This is a real mystery to me. I have an instance variable in a partial > that only gives its value inside a hidden_field_tag. If I try to use > the variable anywhere else in the partial, i either get nil or an > undefined error. The goal is to use the value of @event.id outside of > hidden_field_tag. > > Here''s what works: > <%= hidden_field_tag ''id'', @event && @event.id, :id => ''event_id'' %> > > ...spits out this HTML: <input type="hidden" name="id" id="event_id" > value="771"/> > > So I read that to mean that @event.id is 771. Fine, good.I don''t think it''s doing what you think it''s doing. This may help: http://www.railsforum.com/viewtopic.php?pid=28781 Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---