4

我是一个项目的新手,并试图向已经有一堆移动部件的系统添加一个功能,我认为其中大部分与我试图添加的功能完全无关。

我想向 ActiveAdmin/Formtastic 表单添加字段,以允许用户创建和更新与产品相关的定价方案。每个产品都有一个定价方案,可以有多个定价层。

这些字段显示在表单上,​​但是当我更新产品对象时,定价层对象中的费用和最大属性不会保存到数据库中

ActiveAdmin 表单有一个变量,pricing_scheme_object,它是我根据这个 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

当我保存产品时,我想更新或创建定价方案以及与之关联的任何定价层。

我该怎么做呢?

4

2 回答 2

0

您正在 PUT 获得许可,因此您可能需要accepts_nested_attributes_for :pricing_tiersin 许可?

于 2013-03-21T14:56:09.340 回答
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选项中传递,以告诉它它们应该用于哪些属性。

于 2013-03-21T03:08:01.393 回答