Reputation: 2636
I 'm trying to upload multiple files to S3 using paperclip gem and referring to this tutorial. I 'm having two issues 1. the upload tag is only rendered once instead of 6 times as specified in the controller. 2. And secondly i get the following error whenever i try to edit the document.
ActiveRecord::UnknownAttributeError in DocumentsController#edit
unknown attribute: document_id
Any suggestions how to fix this?
Models/Document.rb
class Document < ActiveRecord::Base
attr_accessible :documentId, :name, :notes, :user_id
belongs_to :user
has_many :photos, :dependent=>:destroy
accepts_nested_attributes_for :photos
acts_as_taggable
validates :name, :length => {:minimum =>3}
validates_date :dtReminder, :on_or_after => lambda { Date.current }, :allow_blank => true
validates_associated :user
validates_uniqueness_of :name, :scope => :user_id
end
Models/Photo.rb
class Photo < ActiveRecord::Base
belongs_to :document
has_attached_file :image, :style =>{:thumb => '150x150#',
:medium => '300x300>',
:large => '600x600>'
}
validates_attachment_size :photo, :less_than => 500.kilobytes
end
Controller/document_Controller.rb
def new
@document = Document.new
@document.user = current_user
5.times {@document.photos.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: @document }
end
end
def create
@document = Document.new(params[:document])
@document.user = current_user
5.times {@document.photos.build}
respond_to do |format|
if @document.save
format.html { redirect_to user_documents_path(current_user), notice: 'Document was successfully created.' }
format.json { render json: @document, status: :created, location: @document }
else
format.html { render action: "new" }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end
Views/documents/_form.html.erb
<%= form_for(document) do |f| %>
<table>
<tr>
<td><%= f.label :name %></td>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<td><%= f.label :notes %></td>
<td><%= f.text_area :notes %></td>
</tr>
<tr>
<td>Photos</td>
<td>
<%= fields_for :photos do |photo|%>
<%= photo.file_field :image%>
<%end%>
</td>
</tr>
<tr>
<td></td>
<td><%= f.submit %></td>
</tr>
</table>
<% end %>
Upvotes: 1
Views: 1033
Reputation: 1377
For multiple image uploading fields, checkout Ryan Bate's screencasts on the a very similar subject.
http://railscasts.com/episodes/73-complex-forms-part-1
http://railscasts.com/episodes/74
http://railscasts.com/episodes/75
Upvotes: 0