Rails - 嵌套表单不保存

时间:2015-10-09 10:08:05

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

我有问题,我无法弄清楚如何解决这个问题,我尝试跟踪代码中的每个字符,但我认为我没有错误的代码

我有3个模型:产品 AttributeProduct AttributeProductRelation ,以及关系:

class Product < ActiveRecord::Base
  has_many :attribute_product_relations
  has_many :attribute_products, through: :attribute_product_relations
  accepts_nested_attributes_for :attribute_product_relations, allow_destroy: true
end

class AttributeProduct < ActiveRecord::Base
  has_many :attribute_product_relations
  has_many :products, through: :attribute_product_relations
end

class AttributeProductRelation < ActiveRecord::Base
  belongs_to :product
  belongs_to :attribute_product
end

在ProductsController上我有这个:

def new
  @product = Product.new(id: generate_product_id)
  2.times { @product.attribute_product_relations.build }
end

def create
  @store = current_user.store
  @product = @store.products.build(new_product_params)
  respond_to do |format|
    if @product.save
      format.html { redirect_to red_path, notice: I18n.t('notice.create_product_success') }
      format.json { render :show, status: :created, location: @product }
    else
      format.html { render :new }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

private 

def new_product_params
  params.require(:product).permit(:id, :name :description, attribute_product_relations_attributes: [:id, :product_id, :attribute_product_id, :value])
end

这是一种形式:

<%= nested_form_for @product, html: { class: "form-horizontal" } do |f| %>
<%#= order stuff here %>
 <%= f.fields_for :attribute_product_relations do |attribute_product_relation_form| %>
  <div class="row">
    <div class="col-xs-6 col-md-6">
       <%= attribute_product_relation_form.hidden_field :attribute_product_id, value: 1 %>
       <label class="control-label">size</label>
    </div>
    <div class="col-xs-6 col-md-6">
       <%= attribute_product_relation_form.text_field :value, {class: "form-control text select_short" %>
    </div>
  </div>
 <% end %>
<% end %>

这是我提交表单时的日志:

Started POST "/products" for 127.0.0.1 at 2015-10-09 16:11:51 +0700
Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WzSKpXm4AFhETrPyv0Ez/WohxoAMMiiRRc6CNhgIPC8=", "product"=>{"id"=>"2132051", "name"=>"T-shirt", "attribute_product_relations_attributes"=>{"0"=>{"attribute_product_id"=>"1", "value"=>"Medium"}, "1"=>{"attribute_product_id"=>"1", "value"=>"SMALL"}} , "description"=>"cool t-shirt"}, "button"=>""}

您可以看到参数中存在attribute_product_relations_attributes,因此我使用pry-debugger gems来跟踪new_product_params方法,我就是这样:

[1] pry(#<ProductsController>)> new_product_params
=> {"id"=>"2132051",
 "name"=>"T-shirt"
 "description"=>"cool t-shirt"}

我在attribute_product_relations_attributes上看不到new_product_params并且没有保存到数据库。

1 个答案:

答案 0 :(得分:0)

由于您在new_product_params方法中有许多关系,因此需要为attribute_product_relations_attributes数组生成许可。所以,你应该重构new_product_params方法

def new_product_params
  params.require(:product).permit(:id, :name :description, attribute_product_relations_attributes: [ [:id, :product_id, :attribute_product_id, :value] ])
end

我认为这会有所帮助。