Spree:新しい資産失敗を作成します
-
02-01-2020 - |
質問
Spree 2.3(Designer)で3番目の役割のためのカスタムフォームを作成しようとしています。設計者は新しい製品とバリアントを作成してから、そのバリアントの画像をアップロードします。私の努力はこれまでのところ無駄になっています。
バリアントのための新しい資産を作成する方法は?
エラー
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 %>
. 解決
画像を持っているモデルに次の関連付けを作成します。
has_many :images, :as => :viewable, :order => :position, :dependent => :destroy, :class_name => "Spree::Image"
.
バリアントモデルの具体的なモデルである方法は、以下のものです。
新しい添付ファイルを作成するためにネストされたフォームを作成します、私はsimple_form
を好みます= simple_form_for @asset, :html => {:multipart => true } do |m|
= m.simple_fields_for :images do |p|
= p.file_field :attachment
.
またはそれをするより簡単な方法では、モデルに論文bundle open 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' }
.
必要に応じてカスタマイズします。
それが助けになることを願っています!
所属していません StackOverflow