Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permit Rack::Test::UploadedFile with strong parameters?

I'm using strong_parameters gem with Rails 3.2. To test an image upload process, I have an incoming post request with parameters:

{"photo"=>{"image"=>#<Rack::Test::UploadedFile:0x000000069ce1f8 @content_type="image/png", @original_filename="test.png", @tempfile=#<Tempfile:/tmp/test.png20130420-9529-1xuka4v-1>>, "status"=>"approved", "in_use"=>false}, "controller"=>"member/photos", "action"=>"create"}

How can I permit the image attribute for assignment? I tried;

params.require(:photo).permit(:image)

but it doesn't work and says Validation failed: Image can't be blank.

When I permit all parameters with params.require(:photo).permit! it works fine.

like image 236
Eren CAY Avatar asked Apr 20 '13 12:04

Eren CAY


1 Answers

From http://api.rubyonrails.org/classes/ActionController/Parameters.html

PERMITTED_SCALAR_TYPES = [ String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, StringIO, IO, ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile]

So you just need to permit it as a scalar type:

params.require(:photo).permit(:image)

Just tested on Rails 4.1.1

like image 66
freemanoid Avatar answered Nov 17 '22 00:11

freemanoid