[英]Rails 4 - How to Access Params
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. 1)将“ url”参数添加到DB 2)对“ filename”参数进行标题化,然后将其提交给DB。
Thanks very much! 非常感谢!
require
works like params[:some_key]
, except it raises ActionController::ParameterMissing
when the value returned is blank. require
工作方式类似于params[:some_key]
,但是当返回的值为空时,它会引发ActionController::ParameterMissing
。
This is handy because it allows, using rescue_from
, to render a 400: bad request
error in this case. 这很方便,因为在这种情况下,它允许使用
rescue_from
呈现400: bad request
错误。
So you have to wrap your params so that when you call require(:choice)
you get a hash. 因此,您必须包装参数,以便在调用
require(:choice)
时获得哈希值。
Unfortunately s3_upload_form
does not seem to support the same builder syntax as form_for
, that would allow you to do : 不幸的是,
s3_upload_form
似乎不支持与form_for
相同的构建器语法,这将允许您执行以下操作:
<%= form_for :s3_upload do |f| %>
<% f.file_field :file %>
<% end %>
resulting in a params hash like this : 导致像这样的params哈希:
{ 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
. 因此,我的建议只是删除
require
调用,并将:choice
和传递给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 : 如果您仍然希望使用
require
方法的好处(即检查是否缺少参数),则始终可以执行以下操作:
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. 如果缺少任何允许的参数,则会引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.