# complete stumper: variable exists in hidden\_field\_tag, but nil everywhere else

**URL:** <https://discuss.rubyonrails.org/t/complete-stumper-variable-exists-in-hidden-field-tag-but-nil-everywhere-else/15044>\
**Category:** rubyonrails-talk\
**Created:** [June 28, 2007, 7:01pm UTC](https://discuss.rubyonrails.org/t/complete-stumper-variable-exists-in-hidden-field-tag-but-nil-everywhere-else/15044 "2007-06-28T19:01:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Frankovitz1](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jason\_Frankovitz1](https://discuss.rubyonrails.org/u/Jason_Frankovitz1)\
**Post date:** [June 28, 2007, 7:01pm UTC](https://discuss.rubyonrails.org/t/complete-stumper-variable-exists-in-hidden-field-tag-but-nil-everywhere-else/15044/1 "2007-06-28T19:01:13Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= hidden\_field\_tag 'id', @event && @event.id, :id =\> 'event\_id' %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= hidden\_field\_tag 'id', @event && @event.id, :id =\> 'event\_id' %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= hidden\_field\_tag 'id', @event && @event.id, :id =\> 'event\_id' %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= hidden\_field\_tag 'id', @event && @event.id, :id =\> 'event\_id' %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= 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

---

<div class="post-metadata">

**Author:** ![Bill\_Walton](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@Bill\_Walton](https://discuss.rubyonrails.org/u/Bill_Walton)\
**Post date:** [June 28, 2007, 8:10pm UTC](https://discuss.rubyonrails.org/t/complete-stumper-variable-exists-in-hidden-field-tag-but-nil-everywhere-else/15044/2 "2007-06-28T20:10:47Z")

</div>

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](http://www.railsforum.com/viewtopic.php?pid=28781)

Best regards, Bill
