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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top