简体   繁体   中英

Rails Nested Form multiple levels

These are the models:

class Question < ActiveRecord::Base
  attr_accessible :title, :description, :inquiries_attributes

  has_many :replies

  has_many :groups, :through => :question_groups
  has_many :question_groups

  has_many :inquiries
  accepts_nested_attributes_for :inquiries, :allow_destroy=>true,
                            :reject_if=>:all_blank

  belongs_to :user
  belongs_to :last_user, :class_name => "User", :foreign_key => "last_user_id"

  acts_as_by_user

  default_scope order("created_at DESC")

  acts_as_publicable

  checkboxes_for :groups

end

class Inquiry < ActiveRecord::Base

  attr_accessible :title, :question_id, :inquiry_type_id, :inquiry_options_attributes

  belongs_to :question
  belongs_to :inquiry_type
  has_many :inquiry_options
  accepts_nested_attributes_for :inquiry_options, :allow_destroy=>true,
                            :reject_if=>:all_blank
  has_many :inquiry_replies
end

class InquiryOption < ActiveRecord::Base

  attr_accessible :content, :inquiry_id

  belongs_to :inquiry

end

This is the view:

<%= nested_form_for [:admin, @question] do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <%= f.fields_for :inquiries do |inquiry_form| %>
    <p>
      <%= inquiry_form.label :title %><br />
      <%= inquiry_form.text_area :title, :size=>"40x5" %><br />
      <%= inquiry_form.label :inquiry_type %><br />
      <%= collection_select(inquiry_form, :inquiry_type_id, InquiryType.all, :id, :name, options ={:prompt => "Seleziona una tipologia"}) %><br />

      <%= inquiry_form.fields_for :inquiry_options do |inquiry_option_form| %>
      <p>
        <%= inquiry_option_form.label :content %><br />
         <%= inquiry_option_form.text_area :content, :size=>"40x5" %><br />
        <%= inquiry_option_form.link_to_remove "Rimuovi risposta" %>
      </p>
    <% end %>
    <p><%= inquiry_form.link_to_add "Aggiungi risposta", :inquiry_options %></p>

     <%= inquiry_form.link_to_remove "Rimuovi domanda" %>
    </p>
  <% end %>
  <p><%= f.link_to_add "Aggiungi domanda", :inquiries %></p>
  <p>
    <%= f.checkboxes_for :groups %>
  </p>
 <p><%= f.submit %></p>
<% end %>

In the second level, if I want to add more than 1 inquiry_option, only the first is saved. I see that when I add more than 1 inquiry_option, the second inquiry_option has the same name attribute of the first inquiry_option and so only the first is saved when I submit he form. How can I solve it?

If you want multiple values, you need to "build" them in the controller. Using your example, you should add the following to your new method in questions_controller.rb

2.times do
  @question.inquiries.build
end

This would build you 2 inquiries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM