Rails 4来自控制台的Ajax POST,强大的params无法正常工作(Rails 4 Ajax POST from console, strong params not working)
我正试图通过AJAX发布。 我的Formal控制器只有一个字段: ids 。
我列出了白色参数:
def formal_params
params.require(:formal).permit(:ids)
end
json = {formal:{ids:["2134"]}}
$.ajax({
url: '/formals/create',
type: 'post',
contentType: 'json',
accept: 'application/json',
dataType: 'json',
data: json
});
我已经尝试了100种变体,并且param is missing or the value is empty: formal一个param is missing or the value is empty: formal每次param is missing or the value is empty: formal 。
我究竟做错了什么? 如果这是一个愚蠢的错误,请告诉我。 我会添加你需要的任何信息......我知道这可能是重复的,但我已经把头发拉了几个小时。 请指教。
I'm trying to post via AJAX. My Formal controller only has one field: ids.
I have the parameters white listed:
def formal_params
params.require(:formal).permit(:ids)
end
json = {formal:{ids:["2134"]}}
$.ajax({
url: '/formals/create',
type: 'post',
contentType: 'json',
accept: 'application/json',
dataType: 'json',
data: json
});
I've tried 100 variations on this and get a param is missing or the value is empty: formal every time.
What am I doing wrong? If it's a dumb mistake, please let me know. I'll add any information you need...and I know this may be a duplicate, but I've been pulling my hair out for hours. Please advise.
原文:https://stackoverflow.com/questions/26847044
更新时间:2019-12-05 12:44
最满意答案
如果ids是一个数组,那么您的强参数应如下所示:
params.require(:formal).permit(ids: [])
要声明params中的值必须是允许的标量值数组,请将键映射到空数组:
params.permit(:id => [])
If ids is an array, your strong parameters should look like this:
params.require(:formal).permit(ids: [])
To declare that the value in params must be an array of permitted scalar values map the key to an empty array:
params.permit(:id => [])
相关问答
放一个f. 在你的领域之前 = f.fields_for :contact_attributes do |ff|
.field.form-group
= ff.label :Contact_Name, for: :contact_name
= ff.text_field :name, class: 'form-control'
= ff.label :Email, for: :contact_email
= ff.tex
...
代替 {
"strongbolt_user_group"=>{
"name"=>"Some test group",
"description"=>"Some test description",
"user_ids"=>{"0"=>{"id"=>"3"}, "1"=>{"id"=>"2"}, "2"=>{"id"=>"5"}},
"role_ids"=>{"0"=>{"id"=>"1"}, "1"=>{"id"=>"2"}}
}
}
你应该发送 {
...
如果ids是一个数组,那么您的强参数应如下所示: params.require(:formal).permit(ids: []) 从强参数文档 : 要声明params中的值必须是允许的标量值数组,请将键映射到空数组: params.permit(:id => []) If ids is an array, your strong parameters should look like this: params.require(:formal).permit(ids: []) From the st
...
当您查看params散列时, name的值将出现在数组中 ,因此以下内容应该有效 def group_params
params.require(:group).permit(:name => [])
end
When you look into the params hash, the values for name coming in an array, so the below should work def group_params
params.require(:group).
...
就这样做 def user_params
params.permit(:username, :password)
end
just do this def user_params
params.permit(:username, :password)
end
您可以通过多种方式查看/跟踪参数 1)尾随你的rails日志文件tail -f log/development.log ,注意rails web服务器通常在启动后开始跟踪控制台 2)在浏览器中引发异常并查看调试输出 3)在浏览器中使用javascript控制台(一些是内置的),也可以考虑使用Firebug插件的Firefox(使用'console')选项卡 拖尾栏记录 对rails服务器发出的每个请求都将显示传递的所有参数,以及接收请求的控制器和操作以及生成的所有sql语句考虑将“awesome_
...
从它的外观来看,可能有几个原因: 您的路线可能未指向控制器 您可能无法在控制器上正确处理请求(导致500服务器错误) 我已经写了这个作为答案,以便了解如何进一步调试: 路线 根据您的说法,您应该设置此路线: #config/routes.rb
resources :planners do
post :events
end
虽然我不认为这是你的问题,但确保它存在是有益的 Developer Console 如果您使用的是Chrome / Firefox,则可以使用开发者控制台 ( righ
...
如果你有这样的参数: params.require(:crecit).permit(:user_id, :status, :listing_id, :message)
这将要求你的参数像这样进来: {
crecit: {
user_id: ...,
status: ...,
etc
}
}
但是你的AJAX发送没有外部crecit哈希的参数。 您可以更改AJAX数据以发送嵌套哈希,也可以保持相同,而不是删除.require(:crecit) 。 即它只是p
...
您可以将其他参数添加到formData data = new FormData()
data.append('csv', fileData)
data.append('path', 'my/path')
$ .ajax()中没有名为params选项 You can add the additional param to the formData data = new FormData()
data.append('csv', fileData)
data.append('path', 'my/p
...
当您在rails中使用remote: true ,您不需要处理ajax调用,因为rails会为您执行此操作。 因此它调用控制器,但它不会加载html,而是加载myMethod.js.erb版本。 所以您需要做的是更改index.html.erb的HTML以使用表单:
...