Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip in Rails 4 - Strong Parameters Forbidden Attributes Error

Having a problem with a Paperclip upload in Rails 4 - failing on ForbiddenAttributesError (strong parameters validation). Have the latest paperclip gem and latest rails 4 gems.

I have a model "Image" with an attached file "upload" in the model:

has_attached_file :upload, :styles => { :review => ["1000x1200>", :png], :thumb => ["100x100>", :png]}, :default_url => "/images/:style/missing.png"

The image model was created with a scaffold, and I added paperclip migrations. The form partial was updated to use

f.file_field :upload

the form generates what appears to be a typical set of paperclip params, with the image param containing the upload. I am also passing a transaction_id in the image model, so it should be permitted. But that's it - the image and the transaction ID.

I expected to be able to write the following in my controller to whitelist my post - but it failed:

def image_params
  params.require(:image).permit(:transaction_id, :upload)
end

So I got more explicit - but that failed too:

def image_params
  params.require(:image).permit(:transaction_id, :upload => [:tempfile, :original_filename, :content_type, :headers])
end

I'm a bit frustrated that Rails 4 is not showing me what ForbiddenAttributesError is failing on in a development environment - it is supposed to be showing the error but it does not - would be a nice patch to ease development. Or perhaps everyone else is getting something that I am missing! Thanks much for the help.

like image 795
Eskim0 Avatar asked Jul 19 '13 03:07

Eskim0


2 Answers

I understand what happened here now - and will leave this up in the hope it helps someone else. I was porting code from a rails 3 project and missed the line that created the image:

@image = current_user.images.new(params[:image])

In rails 4 this is incorrect (I beleive). I updated to

@image = current_user.images.new(image_params)

and that solved my problem.

like image 172
Eskim0 Avatar answered Oct 16 '22 19:10

Eskim0


It looks like your first one should have worked. This is what I use for my projects.

class GalleriesController < ApplicationController

  def new
    @gallery = Gallery.new
  end

  def create
    @user.galleries.new(gallery_params)
  end

  private

  #note cover_image is the name of paperclips attachment filetype(s)
  def gallery_params
    params.require(:gallery).permit(:cover_image)
  end
end
like image 42
oconn Avatar answered Oct 16 '22 21:10

oconn