Rails 使用 has_many :through 编辑联接 table 的附加字段
Rails edit additional fields of a join table using has_many :through
我遇到的问题与此处几乎完全相同:
Rails has_many :through saving additional fields
但是我无法弄清楚我的代码中缺少什么。基本结构如下:有产品,它们可能有附加服务(如清洁或磨锐)。每项服务的基本价格相同,但产品可能有不同的系数。比方说,产品 1 可能有锐化基本价格 * 1.5,产品 2 可能有锐化基本价格 * 2。连接 table 有点愚蠢 - 服务,我想不出一个像样的名字。
products_controller.rb:
def update
@product.update!(product_params)
redirect_to @product
end
def product_params
params.require(:product).permit(
:title, :description, :advertising_text, :fancy_quote, :hot, :hotpic, :product_size_ids,
{ volume_ids: [] }, { color_ids: [] }, { addservice_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id],
images_attributes: [ :image, :product_id ],
servizations_attributes: [:coefficient, :product_id, :addservice_id]
)
end
产品型号(我删掉了不相关的部分):
class Product < ActiveRecord::Base
has_many :servizations
has_many :addservices, through: :servizations
accepts_nested_attributes_for :servizations
before_destroy :ensure_not_referenced_by_any_line_item
结束
附加服务模型:
class Addservice < ActiveRecord::Base
has_many :servizations
has_many :products, through: :servizations
end
服务模式
class Servization < ActiveRecord::Base
belongs_to :product
belongs_to :addservice
end
这是我的 edit.html.haml:
=form_for @product do |f|
=f.label 'Title'
=f.text_field :title
%p
=f.label 'Colors'
=collection_check_boxes(:product, :color_ids, Color.all, :id, :value )
%p
=f.label 'Additional services'
=collection_check_boxes(:product, :addservice_ids, Addservice.all, :id, :title)
=f.fields_for :servization do |s|
=s.label 'Coefficient'
=s.text_field :coefficient
%p
=f.submit class: 'btn btn-default'
我在保存普通服务或仅保存颜色时没有问题,但是当我尝试编辑和保存系数时,我收到以下错误:
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jLN59PIOgpaIQtg2/rVYcK/xUOCFzSMLosfUhOslY9tNNhjFbh16Aa1/qT/8u7LHuhNTLGr2SB7b/BitekuDJQ==", "product"=>{"title"=>"Power MI", "color_ids"=>["1", "2", "3", ""], "addservice_ids"=>["1", ""], "servization"=>{"coefficient"=>"1.5"}}, "commit"=>"Update Product", "id"=>"7"}
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = LIMIT 1 [["id", 7]]
Unpermitted parameter: servization
服务结构:
Servization(id: integer, product_id: integer, addservice_id: integer, coefficient: float, created_at: datetime, updated_at: datetime)
我不明白...我的选项保存得很好,图片也保存得很好。但是附加服务拒绝保存。
编辑 1:
我想我可能已经接近解决方案了。问题是表单中的正确链接。这是我到目前为止想出的:
=form_for @product do |f|
%p
=f.label "Additional Services"
%br
-Addservice.all.each do |addservice|
=check_box_tag "product[addservice_ids][]", addservice.id, @product.addservice_ids.include?(addservice.id)
=addservice.title
=text_field_tag "product[servizations][coefficient][]", addservice.servizations.where(product_id: @product.id)[0].coefficient
%br
%p
=f.submit class: 'btn btn-default'
这是补丁请求:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wD8uMSu0TxvumAWcs1V6GtAFeNgYhbEPEh8HuV8RWucBuk8At6e3jMuldJWxW5Ctxed7FPe+2hprJMuQzn+6GQ==", "product"=>{"title"=>"Флешеченка на тристапяцот гигов", "color_ids"=>["1", "2", "3", ""], "addservice_ids"=>["1", "2"], "servizations"=>{"coefficient"=>["4", "5"]}}, "commit"=>"Update Product", "id"=>"5"}
然而现在我又开始了
Unpermitted parameter: servizations
错误
问题出在这一行
=f.fields_for :servization do |s|
应该是
=f.fields_for :servizations do |s|
更新:
您应该允许 :id
以便 update 正常工作
def product_params
params.require(:product).permit(
:title, :description, :advertising_text, :fancy_quote, :hot, :hotpic, :product_size_ids,
{ volume_ids: [] }, { color_ids: [] }, { addservice_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id],
images_attributes: [ :image, :product_id ],
servizations_attributes: [:id, :coefficient, :product_id, :addservice_id]
)
end
我遇到的问题与此处几乎完全相同: Rails has_many :through saving additional fields
但是我无法弄清楚我的代码中缺少什么。基本结构如下:有产品,它们可能有附加服务(如清洁或磨锐)。每项服务的基本价格相同,但产品可能有不同的系数。比方说,产品 1 可能有锐化基本价格 * 1.5,产品 2 可能有锐化基本价格 * 2。连接 table 有点愚蠢 - 服务,我想不出一个像样的名字。
products_controller.rb:
def update
@product.update!(product_params)
redirect_to @product
end
def product_params
params.require(:product).permit(
:title, :description, :advertising_text, :fancy_quote, :hot, :hotpic, :product_size_ids,
{ volume_ids: [] }, { color_ids: [] }, { addservice_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id],
images_attributes: [ :image, :product_id ],
servizations_attributes: [:coefficient, :product_id, :addservice_id]
)
end
产品型号(我删掉了不相关的部分):
class Product < ActiveRecord::Base
has_many :servizations
has_many :addservices, through: :servizations
accepts_nested_attributes_for :servizations
before_destroy :ensure_not_referenced_by_any_line_item
结束
附加服务模型:
class Addservice < ActiveRecord::Base
has_many :servizations
has_many :products, through: :servizations
end
服务模式
class Servization < ActiveRecord::Base
belongs_to :product
belongs_to :addservice
end
这是我的 edit.html.haml:
=form_for @product do |f|
=f.label 'Title'
=f.text_field :title
%p
=f.label 'Colors'
=collection_check_boxes(:product, :color_ids, Color.all, :id, :value )
%p
=f.label 'Additional services'
=collection_check_boxes(:product, :addservice_ids, Addservice.all, :id, :title)
=f.fields_for :servization do |s|
=s.label 'Coefficient'
=s.text_field :coefficient
%p
=f.submit class: 'btn btn-default'
我在保存普通服务或仅保存颜色时没有问题,但是当我尝试编辑和保存系数时,我收到以下错误:
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jLN59PIOgpaIQtg2/rVYcK/xUOCFzSMLosfUhOslY9tNNhjFbh16Aa1/qT/8u7LHuhNTLGr2SB7b/BitekuDJQ==", "product"=>{"title"=>"Power MI", "color_ids"=>["1", "2", "3", ""], "addservice_ids"=>["1", ""], "servization"=>{"coefficient"=>"1.5"}}, "commit"=>"Update Product", "id"=>"7"}
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = LIMIT 1 [["id", 7]]
Unpermitted parameter: servization
服务结构:
Servization(id: integer, product_id: integer, addservice_id: integer, coefficient: float, created_at: datetime, updated_at: datetime)
我不明白...我的选项保存得很好,图片也保存得很好。但是附加服务拒绝保存。
编辑 1: 我想我可能已经接近解决方案了。问题是表单中的正确链接。这是我到目前为止想出的:
=form_for @product do |f|
%p
=f.label "Additional Services"
%br
-Addservice.all.each do |addservice|
=check_box_tag "product[addservice_ids][]", addservice.id, @product.addservice_ids.include?(addservice.id)
=addservice.title
=text_field_tag "product[servizations][coefficient][]", addservice.servizations.where(product_id: @product.id)[0].coefficient
%br
%p
=f.submit class: 'btn btn-default'
这是补丁请求:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wD8uMSu0TxvumAWcs1V6GtAFeNgYhbEPEh8HuV8RWucBuk8At6e3jMuldJWxW5Ctxed7FPe+2hprJMuQzn+6GQ==", "product"=>{"title"=>"Флешеченка на тристапяцот гигов", "color_ids"=>["1", "2", "3", ""], "addservice_ids"=>["1", "2"], "servizations"=>{"coefficient"=>["4", "5"]}}, "commit"=>"Update Product", "id"=>"5"}
然而现在我又开始了
Unpermitted parameter: servizations
错误
问题出在这一行
=f.fields_for :servization do |s|
应该是
=f.fields_for :servizations do |s|
更新:
您应该允许 :id
以便 update 正常工作
def product_params
params.require(:product).permit(
:title, :description, :advertising_text, :fancy_quote, :hot, :hotpic, :product_size_ids,
{ volume_ids: [] }, { color_ids: [] }, { addservice_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id],
images_attributes: [ :image, :product_id ],
servizations_attributes: [:id, :coefficient, :product_id, :addservice_id]
)
end