Rails 4嵌套表单不创建它接受的对象_ented_attributes_for

时间:2014-04-28 17:56:29

标签: ruby-on-rails-4 parameters nested-forms nested-attributes

我正在努力让类别和子类别正常运作。到目前为止,我可以创建类别并为其添加子类别。当我提交表单时,subategories_attributes发送,但没有创建子类别记录。请帮忙,我把头发拉到这里,没有一个教程似乎有帮助。

category.rb

class Category < ActiveRecord::Base
   has_many :subcategories, :dependent => :destroy
   accepts_nested_attributes_for :subcategories, :reject_if => lambda {|a| 
   a[:content].blank?}
end

subcategory.rb

class Subcategory < ActiveRecord::Base
   belongs_to :category
end

categories_controller.rb

class CategoriesController < ApplicationController
   before_action :set_category, only: [:show, :edit, :update, :destroy]

def index
   @categories = Category.all
end

def show
end

def new
   @category = Category.new
   @category.subcategories.build
end

def edit
end

def create
   @category = Category.new(category_params)

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

def update
   respond_to do |format|
     if @category.update(category_params)
       format.html { redirect_to @category, notice: 'Category was successfully 
       updated.' }
       format.json { head :no_content }
     else
       format.html { render action: 'edit' }
       format.json { render json: @category.errors, status: :unprocessable_entity }
     end
   end
end

def destroy
   @category.destroy
   respond_to do |format|
     format.html { redirect_to categories_url }
     format.json { head :no_content }
   end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
  def category_params
    params.require(:category).permit(:name, subcategories_attributes: [:id, :name, 
    :_destroy])
  end
end

_form.html.erb

<%= nested_form_for(@category) do |f| %>
  <% if @category.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category 
      from being saved:</h2>   
      <ul>
      <% @category.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 :subcategories do |builder|%>
    <p>
      <b>Subcategory</b>
      <%=builder.text_field :name%>
      <%=builder.link_to_remove "Remove"%>
    <p>
  <%end%>
    <p>
      <%=f.link_to_add "Add a Subcategory", :subcategories%>
    </p>    

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

Params Hash(提交时):

  Parameters: {"utf8"=>"✓", 
  "authenticity_token"=>"19FxRnWF1F3BD4QZIkkce4arkOPt/BMkWFw6Z+vpV+8=", "category"=>
  {"name"=>"Test", "subcategories_attributes"=>{"1398706662184"=>{"name"=>"TS1", 
  "_destroy"=>"false"}, "1398706664804"=>{"name"=>"TS2", "_destroy"=>"false"}}}, 
  "commit"=>"Create Category"} 

提前致谢!

1 个答案:

答案 0 :(得分:0)

subcategories_attributes在您的params哈希中正确发送但是其中缺少content个属性。

"subcategories_attributes"=>{"1398706662184"=>{"name"=>"TS1", 
  "_destroy"=>"false"}, "1398706664804"=>{"name"=>"TS2", "_destroy"=>"false"}}

请注意,没有传递content个密钥。 因此,subcategories_attributes中传递的所有子类别记录都是 REJECTED ,因为您在Category模型中指定了条件:

 accepts_nested_attributes_for :subcategories, :reject_if => lambda {|a| 
   a[:content].blank?}

请注意上面的:reject_if => lambda {|a| a[:content].blank?}部分,这将拒绝content缺少的所有记录。

<强>更新

  

当我进行编辑操作时,尽管按预期添加到数据库中,但字段不会显示(并在其他情况下显示为   形式)。

editnew操作中设置变量,如下所示:

def new
 @category = Category.new
 @subcategories = @category.subcategories.build ## Added
end

def edit
  @subcategories = @category.subcategories  ## Added
end

更新fields_for表单中的edit,如下所示:

<%= f.fields_for :subcategories, @subcategories do |builder|%>