开发者

saving values using nested_attributes on ruby

开发者 https://www.devze.com 2023-02-07 11:40 出处:网络
I have nested attributes for say comment. The parent class is post. <% form_for @post do |f| %> ...........

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.


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号