I am uploading images which POSTs the following params:
Parameters: {"url"=>"https://bucket.s3.amazonaws.com/image014.jpg",
"filepath"=>"/uploads%2F1381509934146-1t5a44loikwhr529-1d68490743e05a1287fea45ed4ed9e59%2Fslide0005_image014.jpg",
"filename"=>"slide0005_image014.jpg",
"filesize"=>"30807",
"filetype"=>"image/jpeg",
"unique_id"=>"1t5a44loikwhr529",
"choice"=>["https://bucket.s3.amazonaws.com/image014.jpg"]}
And here is my form:
<%= s3_uploader_form callback_url: choices_url, callback_param: "choice[]", id: "s3-uploader" do %>
<%= file_field_tag :file, multiple: true %>
<button type="submit" class="btn btn-primary start">
<% end %>
And in my controller I have:
#app/controllers/choices_controller
def create
@choice = Choice.create!(choice_params)
if @choice.persisted?
respond_to do |format|
format.html { redirect_to choices_path }
format.json { head :no_content }
end
end
end
def choice_params
params.require(:choice).permit(:url, :filepath, :filename, :filesize, :filetype)
end
But, it returns the error :
NoMethodError (undefined method `permit' for #<Array:0x007f903e380060>):
app/controllers/choices_controller.rb:49:in `choice_params'
app/controllers/choices_controller.rb:24:in `create'
Can you tell me what I am doing wrong here? All I would like to do is:
1) Add the "url" param to the DB 2) Titlelize the "filename" param and then submit that to the DB.
Thanks very much!
require
works like params[:some_key]
, except it raises ActionController::ParameterMissing
when the value returned is blank.
This is handy because it allows, using rescue_from
, to render a 400: bad request
error in this case.
So you have to wrap your params so that when you call require(:choice)
you get a hash.
Unfortunately s3_upload_form
does not seem to support the same builder syntax as form_for
, that would allow you to do :
<%= form_for :s3_upload do |f| %>
<% f.file_field :file %>
<% end %>
resulting in a params hash like this :
{ s3_upload: {file: <SomeFile ...>} }
So my advice would simply be to drop the require
call, and to put :choice
along the other arguments passed to permit
.
If you want to still have the benefits of the require
method (that is, checking if a param is missing), you can always do something along the lines of :
def choice_params
params.permit(:foo, :bar, :baz).tap do |p|
p.each_key{ |k| p.require k }
end
end
this would raise an exception if any of the permitted parameters is missing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.