Reputation: 93
I have this model:
class Project < ApplicationRecord
acts_as_taggable
has_many :documents, dependent: :destroy
accepts_nested_attributes_for :documents
end
and this model:
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
This is my methods of the projects_controller.rb
file:
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
This is my index view of the project:
<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>
When I create a document in document view, the document is created, but when I want to create the documents attributes in the project view it gives me this error:
undefined method 'description' for ActiveRecord::Associations::CollectionProxy []
What am I doing wrong?
Upvotes: 0
Views: 213
Reputation: 947
In your projects_controller.rb
file in project_params
method change :documents
to :documents_attributes
:
def project_params
params.require(:project).permit(:title, :resume, :documents_attributes => [:id, :name, :description, :date, :local, :document])
end
Upvotes: 3