我是一个项目的新手,并试图将功能添加到已经有大量移动部件的系统中,其中大多数我都不相信这些功能与我的功能相关。 ;我试图添加。
我想将字段添加到ActiveAdmin / Formtastic表单,以允许用户创建和更新与产品相关的定价方案。每种产品都有一种定价方案,可以有很多定价层。
字段显示在表单上,但是当我更新产品对象时,定价层对象中的费用和最大属性不会保存到数据库
ActiveAdmin表单有一个变量pricing_scheme_object,它是基于this Stack Overflow Answer创建的。 ActiveAdmin表单如下所示:
ActiveAdmin.register Product do
actions :all, except: [:destroy]
# menu parent, filters, and scopes, none of which are related
form html: {multipart: true} do |f|
f.inputs "Product" do
# unrelated code
pricing_scheme_object = [:pricing_scheme, f.object.pricing_scheme ||
PricingScheme.new]
f.inputs "Pricing Schemes", :for => pricing_scheme_object do |scheme|
scheme.has_many :pricing_tiers do |tier|
tier.input :fee, input_html: { class: 'pricing_scheme_column'}, size: 5
tier.input :max, input_html: { class: 'pricing_scheme_column'}, size: 5
end
end
# unrelated code
end
f.actions
end
# unrelated code
end
保存产品时,我在服务器上看到以下内容:
Started PUT "/admin/products/139" for 127.0.0.1 at 2013-03-20 12:50:43 -0700
Processing by Admin::ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FzyjTkrTF0LvB1K02kebSgqDQWad8c5/bpbIEhFgQpY=", "product"=>{"three_letter_acronym_id"=>"64", "name"=>"test pdf", "description"=>"<p>this is the pdf </p><div class=\"vimiumReset vimiumHUD\" style=\"right: 150px; opacity: 0; display: none; \"></div>", "status"=>"published", "way"=>"pdf", "custom_path"=>"", "pricing_scheme"=>{"pricing_tiers_attributes"=>{"1363809038085"=>{"fee"=>"30", "max"=>"10"}}}, "allocation_rule_number"=>"", "fulfillment_email"=>"", "fulfillment_username"=>"", "fulfillment_password"=>"[FILTERED]", "fulfillment_url"=>"", "shared_secret"=>"", "api_key"=>"", "callback_url"=>""}, "commit"=>"Update Product", "id"=>"139"}
Product Load (0.4ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 139 LIMIT 1
WARNING: Can't mass-assign protected attributes: pricing_scheme
(0.1ms) BEGIN
[paperclip] Saving attachments.
(0.1ms) COMMIT
Redirected to http://localhost:3000/admin/products/139
Completed 302 Found in 119ms (ActiveRecord: 0.0ms)
Started GET "/admin/products/139" for 127.0.0.1 at 2013-03-20 12:50:43 -0700
Processing by Admin::ProductsController#show as HTML
Parameters: {"id"=>"139"}
Product Load (0.3ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 139 LIMIT 1
ThreeLetterAcronym Load (0.4ms) SELECT `three_letter_acronym`.* FROM `three_letter_acronym` WHERE `three_letter_acronym`.`id` = 64 LIMIT 1
Rendered /Users/strand/.rvm/gems/ruby-1.9.3-p392/bundler/gems/active_admin-c3be25319bb6/app/views/active_admin/resource/show.html.arb (27.3ms)
Completed 200 OK in 144ms (Views: 29.0ms | ActiveRecord: 0.6ms)
产品如下所示:
# app/models/product.rb
class Product < ActiveRecord::Base
# … (probably unrelated code)
has_one :pricing_scheme, dependent: :destroy
accepts_nested_attributes_for :pricing_scheme, allow_destroy: true, reject_if: lambda { |a| a[:price].blank? }
# …
attr_accessible :unrelated_attributes,
:pricing_scheme_attributes,
:pricing_scheme_id
# …
end
定价方案模型是:
# app/models/pricing_scheme.rb
class PricingScheme < ActiveRecord::Base
attr_accessible :display_order, :label, :product_id, :max_quantity,
:min_quantity, :price, :category, :pricing_tiers_attributes
has_many :pricing_tiers
belongs_to :product
accepts_nested_attributes_for :pricing_tiers, :allow_destroy => true
def price_for(number)
# logic to determine the price for a number of products based on the pricing
# tiers associated with the pricing scheme.
end
end
定价等级如下:
# app/models/pricing_tier.rb
class PricingTier < ActiveRecord::Base
attr_accessible :fee, :max, :base, :pricing_scheme_id
belongs_to :pricing_scheme
end
当我保存产品时,我想更新或创建定价方案以及与之相关的任何定价层。
我该怎么做?
答案 0 :(得分:0)
尝试添加:pricing_scheme
作为pricing_scheme_object array
中的第一个对象,即
f.inputs "Pricing Schemes", :for => [:pricing_scheme, f.object.pricing_scheme || PricingScheme.new] do |scheme|
scheme.has_many :pricing_tiers do |tier|
tier.input :fee, input_html: { class: 'pricing_scheme_column'}, size: 5
tier.input :max, input_html: { class: 'pricing_scheme_column'}, size: 5
end
end
问题似乎传递的是params[:pricing_scheme]
而不是params[:pricing_scheme_attributes]
,根据您构建输入的答案,字段名称应该在:for
选项中传递给告诉它应该是什么属性。
答案 1 :(得分:0)
您正在使用许可证,因此您可能需要许可证中的accepts_nested_attributes_for :pricing_tiers