Spree:定制产品的关键属性

发布于 2024-11-05 01:51:53 字数 97 浏览 4 评论 0原文

有谁知道是否可以向产品的关键属性集(名称、描述、永久链接、元描述等)添加新属性?我的想法是,我希望在创建产品时使用这些属性,而不是随后通过产品属性添加它们。

谢谢。

Does anyone know if it's possible to add a new attribute to the set of key attributes (Name, Description, Permalink, Meta Description etc) of a product? The idea is that I want to have these attributes available when I create a product instead of adding them afterwards through Product Properties.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-11-12 01:51:53

最简单的方法是通过迁移将属性直接添加到产品模型中。可以通过使用装饰器来添加验证,装饰器是 Spree 中覆盖模型的首选模式。

# in app/models/product_decorator.rb
Product.class_eval do
  validates :some_field, :presence => true
end

另一种选择是为您的扩展字段创建辅助模型。也许 ProductExtension

# in app/models/product_extension.rb
class ProductExtension < ActiveRecord::Base
  belongs_to :product

  validates :some_field, :presence => true
end

# in app/models/product_decorator.rb
Product.class_eval do
  has_one :product_extension
  accepts_nested_attributes_for :product_extension
  delegate :some_field, :to => :product_extension
end

然后在您的产品创建表单中,您可以使用 fields_for 提供这些字段。我认为需要注意的是,在扩展可用之前,您需要拥有创建的产品模型。您可能可以通过产品控制器创建操作中的一些额外逻辑来解决这个问题。

The simplest way would be to add attributes directly to the Product model through migrations. Validations can be added through the use of the decorators, the preferred pattern within Spree for overriding models.

# in app/models/product_decorator.rb
Product.class_eval do
  validates :some_field, :presence => true
end

Another option would be to create a secondary model for your extended fields. Perhaps ProductExtension

# in app/models/product_extension.rb
class ProductExtension < ActiveRecord::Base
  belongs_to :product

  validates :some_field, :presence => true
end

# in app/models/product_decorator.rb
Product.class_eval do
  has_one :product_extension
  accepts_nested_attributes_for :product_extension
  delegate :some_field, :to => :product_extension
end

Then in your product creation forms you can supply these fields with a fields_for. I think one caveat with this is your going to need to have the created Product model before the extension becomes usable. You could probably get around this with some extra logic in the product controllers create action.

十年不长 2024-11-12 01:51:53

我扩展 Spree 产品模型的方法(通过 delegate_belongs_to):

#app/models/product_decorator.rb
Spree::Product.class_eval do
  has_one :product_extension
  accepts_nested_attributes_for :product_extension, :allow_destroy => true
  delegate_belongs_to :product_extension, :some_field
  attr_accessible :some_field
end

#app/models/product_extension.rb
class ProductExtension < ActiveRecord::Base
  belongs_to :product, :class_name => 'Spree::Product'
  attr_accessible :some_field
end

My way to extend Product model for Spree (via delegate_belongs_to):

#app/models/product_decorator.rb
Spree::Product.class_eval do
  has_one :product_extension
  accepts_nested_attributes_for :product_extension, :allow_destroy => true
  delegate_belongs_to :product_extension, :some_field
  attr_accessible :some_field
end

#app/models/product_extension.rb
class ProductExtension < ActiveRecord::Base
  belongs_to :product, :class_name => 'Spree::Product'
  attr_accessible :some_field
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文