Chiubaka
Chiubaka

Reputation: 789

File Handling with Paperclip

I'm using Paperclip in a gem I built for a specific use case. My gem creates an interface for non-programmers to create and edit forms and then allows users to answer those forms.

I want to use Paperclip in order to provide a "File Upload" input type for questions, so my forms are more versatile. However, this means that I need to use the file_field_tag method to display the file input and I need to manually save whatever information is submitted through that input into the appropriate model object. Currently I'm sending the information through with the name question_1 and then trying to pull the uploaded data out with params["question_1"].

My code looks like this:

    answer.update_attributes(upload: params["question_1"])

But I'm getting a No handler found for <image_name> error and I can't figure out what I'm doing wrong. I thought Paperclip handles everything after I pass it the data from a file_field?

Solution:

My form looked like this: <%= form_for @answer_set, multipart: true do %> when it should have looked like this: <%= form_for @answer_set, html: { multipart: true } do %>.

Upvotes: 1

Views: 1013

Answers (1)

earlonrails
earlonrails

Reputation: 5182

I use

has_attached_file :image
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/jpg', 'image/pjeg']

and then:

@upload = Upload.find(params[:id])
@upload.update_attributes(params[:upload])

config/environment.rb

Rails::Initializer.run do |config|
  config.gem "paperclip", version: "~> 2.7"
end

This thread also suggests checking the multi-part on the form https://stackoverflow.com/a/10076046/1354978

answer_form_for @upload, :html => {:multipart => true} do |f|

There are also other possible solutions on that page.

Upvotes: 2

Related Questions