2

嗨,我已经通过 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-嵌套模型形式第 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
4

1 回答 1

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。

于 2013-05-31T22:42:40.023 回答