timpone
timpone

Reputation: 19969

how to assign a posted file to a Paperclip model in the controller

I am trying to have a one-off image upload functionality for part of a site (via ajax). Right now, the posted values look like this:

Processing by ApiPostsController#upload_image as */*
  Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007feb23c28420 @original_filename="yoko.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"yoko.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/58/8l1dcc5n6dd_7mhqvqr_3zm40000gn/T/RackMultipart20140107-70298-1mih9mx>>}
this is auth_token  WgfFAEz3JIApcWDjz2dRgA

I'd like to do something like this in the controller:

  def upload_image
    @asset=Asset.new
    @asset.asset_file_name=params[:filename]
    @asset.save!

    r={}
    r[:status]="success"
    render text: r.to_json
  end

Our Asset model uses asset as name (unfortunately).

mysql> describe assets;
+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| name                 | varchar(255) | YES  |     | NULL    |                |
| created_at           | datetime     | NO   |     | NULL    |                |
| updated_at           | datetime     | NO   |     | NULL    |                |
| asset_file_name      | varchar(255) | YES  |     | NULL    |                |
| asset_content_type   | varchar(255) | YES  |     | NULL    |                |
| asset_file_size      | int(11)      | YES  |     | NULL    |                |
| asset_updated_at     | datetime     | YES  |     | NULL 

I'm trying to follow the readme for paperclip where it says:

Paperclip will wrap up up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:

_file_name _file_size _content_type _updated_at

By default, only _file_name is required for paperclip to operate.

But I get this error

ActiveRecord::RecordInvalid (Validation failed: Asset can't be blank): app/controllers/api_posts_controller.rb:21:in `upload_image'

How could I get paperclip to accept this uploaded file?

Upvotes: 0

Views: 426

Answers (1)

BroiSatse
BroiSatse

Reputation: 44715

Try:

@asset.asset = params[:file]

What is written in a doc means that you can add those four columns to your models and they all be auto-populated on saving an attachment. asset_file_name is the only required column and it will contain the name of new file created on a server. If you assign file_name alone, you will only fill one column in your db, but the file itself is not being saved on server's disk - you need to assign the whole file and paperclip will take care of the rest.

Upvotes: 1

Related Questions