我有这个型号:
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'
我做错了什么?
答案 0 :(得分:3)
在projects_controller.rb
方法更改project_params
至:documents
的{{1}}文件中:
:documents_attributes