Rails has_many嵌套属性+ paperclip gem

时间:2016-12-31 01:20:27

标签: ruby-on-rails ruby paperclip nested-forms

我有这个型号:

class Project < ApplicationRecord
    acts_as_taggable
    has_many :documents, dependent: :destroy
    accepts_nested_attributes_for :documents
end

和这个模型:

class Document < ApplicationRecord
    belongs_to :project,inverse_of: :document, dependent: :destroy
    has_attached_file :document, styles: {thumbnail: "60x60#"}
    validates_attachment :document, content_type: { content_type:     "application/pdf" }
end

这是projects_controller.rb文件的方法:

def new
    @project = Project.new
    @project.documents.build 
end
def project_params
      params.require(:project).permit(:title, :resume, :documents => [:id,:name,:description,:date,:local,:document],
end

这是我对项目的索引视图:

<td><%= project.title %></td>
 <td><%= project.resume %></td>
 <td><%= project.documents.name %></td>
 <td><%= project.documents.description %></td>
 <td><%= project.documents.date %></td>
 <td><%= project.documents.local %></td>
 <td><%= project.documents.document_file_name %></td>
 <td><%= link_to 'Download', project.documents.document.url(:original, false) %> </td>

当我在文档视图中创建文档时,会创建文档,但是当我想在项目视图中创建文档属性时,它会给我这个错误:

  

ActiveRecord :: Associations :: CollectionProxy []

的未定义方法'description'

我做错了什么?

1 个答案:

答案 0 :(得分:3)

projects_controller.rb方法更改project_params:documents的{​​{1}}文件中:

:documents_attributes