为什么“未定义的方法`创建'用于#<array:...”using =“”paperclip =“”multiple =“”upload?=“”

时间:2017-08-03 12:38:43

标签: ruby-on-rails ruby paperclip

=“”< p =“”>我正在尝试通过表单为模型logos保存多个cover。 在本教程:Handling file upload with ROR 5之后,我创建了一个模型logos并正确设置了logoscover之间的关联

我无法解决的问题是当我试图保存我得到的cover实例时:

  
    

enter image description here

  

错误似乎出现在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,我会得到此

enter image description here

1 个答案:

答案 0 :(得分:0)

Cover模型中,尝试将attr_accessor :logos的名称更改为其他内容