1

I have nested attributes for say comment. The parent class is post.

<% form_for @post do |f| %>

...........

<% f.fields_for :comments do |builder| %>

   <%= builder.text_field :name %>
   <%= builder.text_field :address %>
   <%= builder.hidden_field :label, :value => user_1 %>

   <%= builder.text_field :name %>
   <%= builder.text_field :address %>
   <%= builder.hidden_field :label, :value => user_2 %>
   <% end %>

<% end %>

When I save posts I want to save the comments also, And I need the different values for hidden_field, if I use form tag, I don't know how will it save the comments without doing anything in the controller.

I have also used :accepts_nested_attributes_for :comment in the post model. If anybody could give it a second thought, that'll be great.

name, address and label are the fields of comment. When I save post, I need two rows of comments to be saved. One from first text_field, text_field and hidden_field and another row from the second input fields.

1
  • How are you trying to save these nested models? can you post some code here to get a better idea? Commented Jan 31, 2011 at 11:59

2 Answers 2

1

You create a form which has fields with the same ids. This results in invalid HTML and is not handled correctly by all (most) browsers.

You need to generate unique ids, to to this use FormTagHelpers. Eg:

<% form_tag('/builder') %>
  <%= text_field_tag ':name1' %>
  <%= text_field_tag ':address1' %>

  <%= text_field_tag 'name2' %>
  <%= text_field_tag 'address2' %>
<% end %>

This will generate correct HTML. Now you need to change your controller to handle the differently names fields. You'd need to use params[:name1] and params[:name2] to get the name of user1 and user2.

By changing the ids into something more useful (I do not remember by head how the ids should be names atm...) you can 'group' the fields for the users so you can use something like params[:user1][:name] which might match better with your table.

Edit: Did you already had a look at the field_for documentation? It has some nice examples of the Models and the required forms.

Sign up to request clarification or add additional context in comments.

Comments

1

I fixed this one with the help of my friend I have this code in my controller

if @post.comments.nil?
  @post.comments.build(:username => "user1") 
  @post.comments.build(:username => "user2") 
end

And have this in my form

<%= form_for @post do |f| %>
  <%= f.fields_for :comments do |builder| %>
    <%= builder.label :username, "#{builder.object.username}" %>
    <%= builder.hidden_field :username %>
    <% if builder.object.username == "user1" %>
      <%= builder.text_field :address, :value => "address1" %>
    <% else %>
      <%= builder.text_field :address, :value => "address2" %>
    <% end %>
  <% end %>
<%= f.submit "Create Post" %>
<%end%>

So, what happens is when I create post, two comments 1st with value address:address1, username:user1 and 2nd with value address:address2, username: user2 gets saved when saving post!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.