我是Spreecommerce的新手,我在创建具有自定义属性的产品时遇到问题' delivery_on'通过管理界面。简而言之,我创建了migrate,product_decorator.rb,将新属性添加到允许的属性,为管理页面创建一个deface文件。但是,我收到一条错误消息,说明"交货时无法生效"当我尝试通过管理界面创建新产品时。
非常感谢您的任何帮助!
我使用ruby 2.2.1,rails 4.2.3和以下狂欢宝石:
gem 'spree_core', branch: '3-0-stable'
gem 'spree_api', branch: '3-0-stable'
gem 'spree_backend', branch: '3-0-stable'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'devise'
重现的步骤
rails g migration add_delivery_on_to_spree_products
在迁移文件YYYYMMDDHHMMSS_add_delivery_on_to_spree_products中,插入更改以更改'方法:
def change
add_column :spree_products, :delivery_on, :date
add_index :spree_products, :delivery_on
end
bundle exec rake db:migrate
并将属性添加到spree_products表中,但它不会更新scheema.rb 运行rake db:migrate:reset
并更新架构。但它首先消灭了数据库。不确定这是理想的做法(遵循stackoverflow thread上的指导)
在app / models / spree文件夹中,创建' product_decorator.rb'文件并添加
Spree::Product.class_eval do
validates :delivery_on, presence: true
delegate_belongs_to :master, :delivery_on, :active
end
在app / controllers / application_controllers.rb中,添加
Spree::PermittedAttributes.product_attributes << :delivery_on
Spree::Api::ApiHelpers.product_attributes << :delivery_on
在app / overrides / spree中,创建deface文件&#39; add_delivery_on_to_product_new&#39;:
Deface::Override.new(:virtual_path => 'spree/admin/products/new',
:name => 'add_delivery_on_to_product_new',
:original => '931806c52c232e6a373cb06fa739f3913ad13c3d',
:insert_after => "erb[loud]:contains('text_field :available_on')",
:text => "
<%= f.field_container :delivery_on, class: ['form-group'] do %>
<%= f.label :delivery_on, raw(Spree.t(:delivery_on) + content_tag(:span, ' *')) %>
<%= f.error_message_on :delivery_on %>
<%= f.text_field :delivery_on, :class => 'datepicker form-control' %>
<% end %>
")
在app / overrides / spree中,创建deface文件&#39; add_delivery_on_to_product_form&#39;:
Deface::Override.new(:virtual_path => 'spree/admin/products/_form',
:name => 'add_delivery_on_to_product_edit',
:original => '931806c52c232e6a373cb06fa739f3913ad13c3d',
:insert_after => "erb[loud]:contains('text_field :available_on')",
:text => "
<%= f.field_container :delivery_on, class: ['form-group'] do %>
<%= f.label :delivery_on, raw(Spree.t(:delivery_on) + content_tag(:span, ' *')) %>
<%= f.error_message_on :delivery_on %>
<%= f.text_field :delivery_on, :value =>
datepicker_field_value(@product.delivery_on), class: 'datepicker form-control' %>
<% end %>
")
转到localhost:3000 / admin并尝试创建新产品,但我收到一条错误消息,说明&#34;交付时不能为空。&#34;
以下是日志中的输出
开始发布&#34; / admin / products&#34; for :: 1 at 2015-09-14 22:24:20 +0200 由Spree处理:: Admin :: ProductsController#创建为HTML 参数:{&#34; UTF8&#34; =&GT;&#34;✓&#34;,&#34; authenticity_token&#34; =&GT;&#34; O6BkeJEhquvCnu5nrIHwYYUZqkif8C9MqKRf4 + qvjxH5UqkM9m + QDwvxcfBFqE4QcwPnDU / wrwAKGyk + wzVz8Q == &#34;,&#34;产品&#34; =&gt; {&#34;名称&#34; =&gt;&#34;测试&#34;,&#34; sku&#34; =&gt;&# 34; 123&#34;,&#34; prototype_id&#34; =&gt;&#34;&#34;,&#34;价格&#34; =&gt;&#34; 99&#34;,&#34 ; available_on&#34; =&gt;&#34; 2015/09/12&#34;,&#34; delivery_on&#34; =&gt;&#34; 2015/09/30&#34;,&#34; shipping_category_id& #34; =&gt;&#34; 1&#34;},&#34;按钮&#34; =&gt;&#34;&#34;} Spree :: Preference Load(0.1ms)SELECT&#34; spree_preferences&#34;。* FROM&#34; spree_preferences&#34;在哪里&#34; spree_preferences&#34;。&#34; key&#34; =? LIMIT 1 [[&#34; key&#34;,&#34; spree / backend_configuration / locale&#34;]] 用户负载(0.4ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; =? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC限制1 [[&#34; id&#34;,1]] (0.1ms)SELECT COUNT(*)FROM&#34; spree_roles&#34; INNER JOIN&#34; spree_roles_users&#34; ON&#34; spree_roles&#34;。&#34; id&#34; =&#34; spree_roles_users&#34;。&#34; role_id&#34;在哪里&#34; spree_roles_users&#34;。&#34; user_id&#34; =? AND&#34; spree_roles&#34;。&#34; name&#34; =? [[&#34; user_id&#34;,1],[&#34; name&#34;,&#34; admin&#34;]] (0.2ms)开始交易 Spree ::产品存在(1.1ms)SELECT 1 AS AS FROM&#34; spree_products&#34; INNER JOIN&#34; friendly_id_slugs&#34; ON&#34; friendly_id_slugs&#34;。&#34; sluggable_id&#34; =&#34; spree_products&#34;。&#34; id&#34; AND&#34; friendly_id_slugs&#34;。&#34; deleted_at&#34; IS NULL和&#34; friendly_id_slugs&#34;。&#34; sluggable_type&#34; =? WHERE(&#34; spree_products&#34;。&#34; id&#34; IS not NULL)和&#34; spree_products&#34;。&#34; slug&#34; =&#39;测试&#39; LIMIT 1 [[&#34; sluggable_type&#34;,&#34; Spree :: Product&#34;]] Spree ::产品存在(0.2ms)SELECT 1 AS AS FROM&#34; spree_products&#34; INNER JOIN&#34; friendly_id_slugs&#34; ON&#34; friendly_id_slugs&#34;。&#34; sluggable_id&#34; =&#34; spree_products&#34;。&#34; id&#34; AND&#34; friendly_id_slugs&#34;。&#34; deleted_at&#34; IS NULL和&#34; friendly_id_slugs&#34;。&#34; sluggable_type&#34; =? WHERE(&#34; spree_products&#34;。&#34; id&#34; IS not NULL)和&#34; spree_products&#34;。&#34; slug&#34; =&#39; test-123&#39; LIMIT 1 [[&#34; sluggable_type&#34;,&#34; Spree :: Product&#34;]] Spree ::产品存在(0.5ms)SELECT 1 AS AS FROM&#34; spree_products&#34; INNER JOIN&#34; friendly_id_slugs&#34; ON&#34; friendly_id_slugs&#34;。&#34; sluggable_id&#34; =&#34; spree_products&#34;。&#34; id&#34; AND&#34; friendly_id_slugs&#34;。&#34; deleted_at&#34; IS NULL和&#34; friendly_id_slugs&#34;。&#34; sluggable_type&#34; =? WHERE(&#34; spree_products&#34;。&#34; id&#34; IS not NULL)和(&#34; friendly_id_slugs&#34;。&#34; sluggable_type&#34; =&#39; Spree ::产品&#39; AND&#34; friendly_id_slugs&#34;。&#34; slug&#34; =&#39; test-123&#39;)LIMIT 1 [[&#34; sluggable_type&#34;,&#34 ;施普雷::产品&#34;]] Spree :: Variant Exists(0.5ms)SELECT 1 AS as FROM&#34; spree_variants&#34;在哪里&#34; spree_variants&#34;。&#34; sku&#34; =&#39; 123&#39; AND&#34; spree_variants&#34;。&#34; deleted_at&#34; IS NULL LIMIT 1 Spree ::产品存在(0.1ms)SELECT 1 AS AS FROM&#34; spree_products&#34;在哪里&#34; spree_products&#34;。&#34; slug&#34; =&#39; test-123&#39;限制1 (0.1ms)回滚交易
的Gemfile
source 'https://rubygems.org'
ruby '2.2.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spree_core', branch: '3-0-stable'
gem 'spree_api', branch: '3-0-stable'
gem 'spree_backend', branch: '3-0-stable'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'devise'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
group :production do
# Use PostgreSQL as the database for Active Record
gem 'pg', '0.18.2'
# Use rails_12 factor to enable serving assets in production and setting your logger to standard out.
gem 'rails_12factor', '0.0.3'
end
答案 0 :(得分:0)
我删除了
delegate_belongs_to :product, :delivery_on
来自我的product_decorator.rb文件,解决了这个问题。
说实话,我并没有真正理解delegate_belongs_to的作用,但我添加了它,因为官方Spree deface overrides tutorial指示我们这样做:
我们还需要将sale_price委托给主变体以便 获取更新的产品编辑表单。
我们可以通过创建新文件来完成此操作 app / models / spree / product_decorator.rb并添加以下内容 它:
模块Spree Product.class_eval做 delegate_belongs_to:master,:sale_price end end
注意,我尝试过delegate_belongs_to:master,:delivery_on,但也没有。