Rails演员#196 /#197嵌套模型生成

时间:2013-05-30 15:00:51

标签: ruby-on-rails nested-attributes railscasts

嗨,我已经走了Railscast 196& 197并使一切正常,但我有一个关于扩展功能的问题。

在Railscast 196& 197(http://railscasts.com/episodes/197-nested-model-form-part-2)Ryan添加了一个问题然后单独添加答案。我试图实现的是在添加问题时自动添加答案。

以下是项目代码:http://railscasts.com/episodes/196-nested-model-form-revised(如果您没有订阅,则为旧版本:http://railscasts.com/episodes/197-nested-model-form-part-2

评论部分中有一条评论建议将行child_object = f.object.class.reflect_on_association(association).klass.reflect_on_association(child_association).klass.new添加到帮助程序中以实现此目的,但是当我实现此操作时,我收到错误:

undefined method 'klass' for nil:NilClass

编辑:要清楚,尝试构建child_object时,此错误会失败。 new_object将正确构建和呈现。

我的修改好帮手

def link_to_add_fields(name, f, association, container, child_association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  child_object = f.object.class.reflect_on_association(association).klass.reflect_on_association(child_association).klass.new
  new_object.answers = child_object

  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s + "/" + association.to_s.singularize + "_fields", :f => builder)
  end

  link_to name, "#", class: "add-fields", data: { container: "#{container}", association: "#{association}", content: "#{fields}" }
end

致电帮助

= form_for @survey, :html => { :multipart => true } do |f|
  %fieldset
    %legend
      Questions
    #questions.nested
      = f.fields_for :questions do |question|
        = render 'questions/question_fields', :f => question
    = link_to_add_fields "Add Question", f, :questions, "questions", :image

1 个答案:

答案 0 :(得分:4)

好的,终于把这弄明白了!

<强>表格

<%= form_for @survey do |f| %>
  <% if @survey.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
      <ul>
      <% @survey.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :questions do |builder| %>
    <%= render 'question_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add Question", f, :questions, :answers %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<强>辅助

module ApplicationHelper
  def link_to_add_fields(name, f, association, child_association = nil)
    new_object = f.object.send(association).klass.new

    if child_association
      3.times{ new_object.send(child_association).build } #Builds 3 Answers
      #new_object.send(child_association).build #Builds 1 Answer
    end

    id = new_object.object_id

    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end

    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

child_association = nil

if child_association
  3.times{ new_object.send(child_association).build } #Builds 3 Answers
  #new_object.send(child_association).build #Builds 1 Answer
end

因为通过链接添加新答案也使用相同的帮助器,但由于答案不接受任何嵌套属性,因此它将失败。添加上述内容后,您可以为两者使用相同的帮助程序,并保持DRY。