logos
保存多个cover
。
在本教程:Handling file upload with ROR 5之后,我创建了一个模型logos
并正确设置了logos
和cover
之间的关联
我无法解决的问题是当我试图保存我得到的cover
实例时:
错误似乎出现在save attachment
方法......
这是我的代码:
盖/ _form
= simple_form_for [current_user, @book, @cover], html: { multipart: true } do |f|
= f.label :logos, 'Votre logo', class: "field-label center"
= f.file_field :logos, multiple: true, class: 'text input-border hidden-always'
= f.submit "enregistrer", class: "button w-button", data_wait: "Patientez svp"
logo.rb
class Logo < ApplicationRecord
has_attached_file :file
belongs_to :cover
do_not_validate_attachment_file_type :file
end
cover.rb
class Cover < ApplicationRecord
has_many :logos, dependent: :destroy
attr_accessor :logos
def save_attachments(params)
params[:logos].each do |doc|
self.logos.create(:file => doc)
end
end
end
covers_controller.rb
class CoversController < ApplicationController
def create
if @cover.save
@cover.save_attachments(cover_params) if params[:cover][:logos]
...
end
end
private
def cover_params
params.require(:cover).permit(:name, logos: [])
end
end
end
更新
如果我将attr_accessor :logos
的值更改为attr_accessor :logos_data
,我会得到此
答案 0 :(得分:0)
在Cover
模型中,尝试将attr_accessor :logos
的名称更改为其他内容