# Hidden\_field vs. form\_for Parameter

**URL:** <https://www.ruby-forum.com/t/hidden-field-vs-form-for-parameter/191295>\
**Category:** Rails\
**Created:** [July 9, 2010, 3:56am UTC](https://www.ruby-forum.com/t/hidden-field-vs-form-for-parameter/191295 "2010-07-09T03:56:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![punkrockgeekboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@punkrockgeekboy](https://www.ruby-forum.com/u/punkrockgeekboy)\
**Post date:** [July 9, 2010, 3:56am UTC](https://www.ruby-forum.com/t/hidden-field-vs-form-for-parameter/191295/1 "2010-07-09T03:56:51Z")

</div>

Hi,

I have a question regarding the best way to do the following:

Given I have a Parent model that has many Child models, and the “show”  
page for the parent model allows the user to create a new Child, which  
is preferable?

A) \<% form\_for(@child, :parent\_id =\> @parent.id) do |f| %\>  
f.text\_field :foo  
f.text\_field :bar  
\<% end %\>

– OR –

B) \<% form\_for(@child) do |f| %\>  
f.hidden\_field :parent\_id, :value =\> @parent.id  
f.text\_field :foo  
f.text\_field :bar  
\<% end %\>

AFAIK both ways accomplish the same thing, I’m just wondering if there  
was a preferred “Rails” way.

Thanks,  
Bob

---

<div class="post-metadata">

**Author:** ![punkrockgeekboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@punkrockgeekboy](https://www.ruby-forum.com/u/punkrockgeekboy)\
**Post date:** [July 9, 2010, 4:43am UTC](https://www.ruby-forum.com/t/hidden-field-vs-form-for-parameter/191295/2 "2010-07-09T04:43:41Z")

</div>

If I understand you question, the first. The second puts unnecerssary  
hidden field on the form and the form helper already has the I’d  
embeded. Only reason you would want the hidden field is if you needed  
it perhaps for ajax/js purposes.

On 7/8/10, Bob N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> f.text_field :bar
> 
> ```
> 
> AFAIK both ways accomplish the same thing, I’m just wondering if there  
> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Sent from my mobile device

---

<div class="post-metadata">

**Author:** ![punkrockgeekboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@punkrockgeekboy](https://www.ruby-forum.com/u/punkrockgeekboy)\
**Post date:** [July 9, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/hidden-field-vs-form-for-parameter/191295/3 "2010-07-09T12:26:05Z")

</div>

On 9 July 2010 03:42, DK [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I understand you question, the first. The second puts unnecerssary  
> hidden field on the form and the form helper already has the I’d  
> embeded. Only reason you would want the hidden field is if you needed  
> it perhaps for ajax/js purposes.

DK: Why will @parent.id will be embedded automatically? It is not  
@child.parent.id that is required.

Colin
