English 中文(简体)
形形多层
原标题:Rails Nested Form multiple levels

这些是模型:

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

这一观点是:

<%= 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?

问题回答

如果您想要多个值, 就需要在控制器中“ 创建” 。 以您为例, 您应该将以下内容添加到您的 < code> new 方法中, 在问询_ controller. rb 中 。

2.times do
  @question.inquiries.build
end

这样你就能进行两个调查了





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签