我正在尝试为Spree 2.3(Designer)中的第三个角色创建自定义表单。设计人员创建新产品和变体,然后上传该变体的图像。到目前为止,我的努力都是徒劳的。
如何为变体创建新资源?
错误
ActiveRecord::UnknownAttributeError in Designers::SpreeAssetsController#create
unknown attribute: attachment
Extracted source (around line #11):
def create
@asset = Spree::Asset.new(asset_params) #line 11
...
end
请求参数
{"utf8"=>"✓",
"authenticity_token"=>"XXX=",
"asset"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x0000010f6c34b8 @tempfile=#<Tempfile:/var/folders/xk/r14w_thd2bn8vxch294zzn040000gn/T/RackMultipart20140731-5542-fgfzhn>,
@original_filename="DSCN0220.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"asset[attachment]\"; filename=\"DSCN0220.JPG\"\r\nContent-Type: image/jpeg\r\n">},
"commit"=>"Create"}
控制器
#app/controllers/designers/spree_assets_controller.rb
def create
@asset = Spree::Asset.new(asset_params)
...
end
private
def asset_params
params.require(:asset).permit(:attachment)
end
查看
<%= form_for @asset, url: designers_spree_assets_path, method: :post do |f| %>
<%= f.label :attachment, "Upload image" %></br>
<%= f.file_field :attachment %></br>
<%= f.submit 'Create' %>
<% end %>
答案 0 :(得分:1)
在模型中创建具有图像的以下关联:
has_many :images, :as => :viewable, :order => :position, :dependent => :destroy, :class_name => "Spree::Image"
Variant模型具有以下功能:图像,执行bundle open spree
以获取更多信息。
然后创建一个嵌套表单来创建新附件,我更喜欢simple_form
= simple_form_for @asset, :html => {:multipart => true } do |m|
= m.simple_fields_for :images do |p|
= p.file_field :attachment
或者更简单的方法是在模型中包含回形针has_attached_file
方法。
以下内容来自Image模型,只是为了让您了解如何在Spree中处理产品图像。
has_attached_file :attachment,
:styles => { :mini => '48x48>', :small => '100x100>', :product => '240x240>', :large => '600x600>' },
:default_style => :product,
:url => '/spree/products/:id/:style/:basename.:extension',
:path => ':rails_root/public/spree/products/:id/:style/:basename.:extension',
:convert_options => { :all => '-strip -auto-orient' }
根据需要自定义。
希望它有所帮助!