Hidden_field vs. form_for Parameter

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

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. [email protected] wrote:

 f.text_field :bar

AFAIK both ways accomplish the same thing, I’m just wondering if there
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Sent from my mobile device

On 9 July 2010 03:42, DK [email protected] 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