Paperclip in Rails 4 - Strong Parameters Forbidden Attributes Error
Asked Answered
A

2

10

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.

Arielariela answered 19/7, 2013 at 3:36 Comment(0)
A
9

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.

Arielariela answered 20/7, 2013 at 23:24 Comment(3)
What did you end up putting in your params.permit() to get this to work?Hazan
in my case I had a model "Image" with a paperclip attachment 'image_file'. The paperclip migration creates several columns in the db, such as image_file_file_name etc, but all you have to do is params.require(:image).permit(:image_file) to allow paperclip to work its magic.Arielariela
Thanks so much! That is not intuitive, but it works :)Shirashirah
G
1

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
Gaucho answered 16/3, 2014 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.