ruby on rails - using fields_for & paperclip multiple file update -
i 'm trying upload multiple files s3 using paperclip gem , referring this tutorial. 'm having 2 issues 1. upload tag rendered once instead of 6 times specified in controller. 2. , secondly following error whenever try edit document.
activerecord::unknownattributeerror in documentscontroller#edit
unknown attribute: document_id
any suggestions how 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 |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 |format| if @document.save format.html { redirect_to user_documents_path(current_user), notice: 'document 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) |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 |photo|%> <%= photo.file_field :image%> <%end%> </td> </tr> <tr> <td></td> <td><%= f.submit %></td> </tr> </table> <% end %>
for multiple image uploading fields, checkout ryan bate's screencasts on similar subject.
http://railscasts.com/episodes/73-complex-forms-part-1
Comments
Post a Comment