Rails 4中的嵌套表单似乎不起作用

时间:2015-01-10 01:00:19

标签: ruby-on-rails ruby ruby-on-rails-4

我一直在看这个太长时间,所以这里。

  • 尝试做一个简单的嵌套表单(在Rails 3中很多次之前完成)但我似乎无法使用HTML渲染嵌套字段。
  • 正如您所看到的,我正在尝试使用3.times { @collection.collections_components.build }构建3个'集合组件'(我的模型)。我确实得到了一个字段,但忽略了3.times
  • 似乎忽略了f.fields_for :collection_components。它可以更改为f.fields_for :xyz,并执行相同的一个字段。

在此处:http://guides.rubyonrails.org/form_helpers.html#nested-forms

以下是一些代码:

collection.rb

class Collection < ActiveRecord::Base
    has_many :collections_components, dependent: :destroy
    accepts_nested_attributes_for :collections_components, allow_destroy: true
end

collections_component.rb

class CollectionsComponent < ActiveRecord::Base
    belongs_to :collection
end

_form.html.erb

<%= form_for @collection do |f| %>
  <% if @collection.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@collection.errors.count, "error") %> prohibited this collection from being saved:</h2>

      <ul>
      <% @collection.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

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

  <%= f.fields_for :collection_components do |collection_components_form| %>
    <h2>Collection</h2>
    <%= collection_components_form.label :text %>
    <%= collection_components_form.text_field :text %>
  <% end %>

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

collections_controller.rb

class CollectionsController < ApplicationController
  before_action :set_collection, only: [:show, :edit, :update, :destroy]

  def index
    @collections = Collection.all
  end

  def show
  end

  def new
    @collection = Collection.new
    3.times { @collection.collections_components.build }
  end

  def edit
  end

  def create
    @collection = Collection.new(collection_params)

    respond_to do |format|
      if @collection.save
        format.html { redirect_to @collection, notice: 'Collection was successfully created.' }
        format.json { render :show, status: :created, location: @collection }
      else
        format.html { render :new }
        format.json { render json: @collection.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @collection.update(collection_params)
        format.html { redirect_to @collection, notice: 'Collection was successfully updated.' }
        format.json { render :show, status: :ok, location: @collection }
      else
        format.html { render :edit }
        format.json { render json: @collection.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @collection.destroy
    respond_to do |format|
      format.html { redirect_to collections_url, notice: 'Collection was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_collection
      @collection = Collection.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def collection_params
      params.require(:collection).permit(:name, collections_components_attributes: [:text])
    end
end

2 个答案:

答案 0 :(得分:1)

我认为这可能是问题,你在_form(在收集名称的中间)将资源称为单数而不是复数

<%= f.fields_for :collection_components do |collection_components_form| %> 

而不是:

<%= f.fields_for :collections_components do |collection_components_form| %>

同样在我知道你需要传递“id”之前我一直在使用nested_forms,并且只有当你允许将一个“_destroy”参数销毁给strong_parameters时才会这样,所以它对你有好处添加它

def collection_params
  params.require(:collection).permit(:name, collections_components_attributes: [:text])
end

所以你应该有这样的东西:

def collection_params
  params.require(:collection).permit(:name, {collections_components_attributes: [:id, :text, :_destroy]})
end

此外,我建议将此railscasts用于未来研究,并建议使用名为cocoon的优秀宝石轻松制作嵌套_

我希望它有所帮助!

答案 1 :(得分:0)

您需要显示所有collections_components的表单。变化:

<%= f.fields_for :collection_components do |collection_components_form| %>
  <h2>Collection</h2>
  <%= collection_components_form.label :text %>
  <%= collection_components_form.text_field :text %>
<% end %>

为:

<% @collection.collection_components.each do |collections_component| %>
  <%= f.fields_for :collection_components, collections_component do |collection_components_form| %>
    <h2>Collection</h2>
    <%= collection_components_form.hidden_field :id %>
    <%= collection_components_form.label :text %>
    <%= collection_components_form.text_field :text %>
  <% end %>
<% end %>

id的隐藏字段是更新所必需的。还要确保它包含在强参数中。

修改

我在使用Rails 4.0.8时遇到了这个问题。版本的文档suggests this solution with each。 Rails 4.2.0的文档开始建议仅使用fields_for。我怀疑fields_for直接在4.2.0中开始处理集合,但我找不到任何更改日志。