gpt4 book ai didi

ruby-on-rails-3 - ActiveAdmin 表单不允许批量分配

转载 作者:行者123 更新时间:2023-12-04 18:35:29 25 4
gpt4 key购买 nike

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

我想向 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

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

我该怎么做呢?

最佳答案

尝试添加 :pricing_scheme作为 pricing_scheme_object array 中的第一个对象, IE。

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 中传递选项来告诉它它们应该用于什么属性。

关于ruby-on-rails-3 - ActiveAdmin 表单不允许批量分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com