簡體   English   中英

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

[英]nested forms in Rails 4 doesn't seem to work

我已經看了太久了,所以去吧。

  • 嘗試做一個簡單的嵌套表單(在Rails 3中已經做過很多次了),但是我似乎無法讓nest字段以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

我認為這可能是問題所在,您正在以_form(在集合名稱的中間)將資源稱為單數而不是復數

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

代替這個:

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

就像我一直在使用nested_forms之前,我知道您將需要傳遞“ id”並且僅當您允許破壞屬性的“ _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的出色寶石來輕松制作nested_forms

希望對您有所幫助!

您需要顯示所有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中遇到了這個問題。 該版本的文檔建議each此解決方案 Rails 4.2.0的文檔開始建議僅使用fields_for 我懷疑fields_for開始直接在4.2.0中處理收集,盡管我找不到任何更改日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM