【发布时间】:2019-06-13 16:01:56
【问题描述】:
我正在通过fetch 将表单修补到 Rails,但服务器端未正确处理某些属性。
我正在通过 Ruby
ruby 2.5.1p57运行 Rails5.2.2
当我将数据发布到服务器时,我在浏览器中得到这个console.log 输出:
{id: 10172, weekday: 1, is_only_private: false, is_allow_forced: false, from_time: 08:00, to_time: 09:00, act_ids: [10001, 10002], customer_id: 10000, consultation_id: 10000}
但是在服务器端我可以在控制台上看到这个日志:
Parameters: {"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00", "act_ids"=>[10001, 10002], "customer_id"=>"10000", "consultation_id"=>"10000", "timetable"=>{"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00"}}
act_ids在timetable属性中消失
我的应用是混合的,它在相同的路由中使用 HTML 和 JSON(甚至是 XML)进行响应。
问题:
这个版本的 Rails 还没解决吗?
解决方法
def timetable_params
my_params = params.require(:timetable).permit :weekday,
:is_only_private,
:is_allow_forced,
:from_time,
:to_time,
act_ids: []
my_params[:act_ids] ||= params[:act_ids]
my_params
end
【问题讨论】:
-
这个请求的
request.request_parameters是什么? -
@Vasfed: {"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08: 00", "to_time"=>"09:00", "act_ids"=>[10001, 10002], "customer_id"=>"10000", "consultation_id"=>"10000"}
-
您能否创建一个拼写错误,并在您提交表单时验证参数中显示的内容?您如何在表单上声明 act_ids?
-
@GuilhermeNunes;错字???字段或值中的错字?
-
@GuilhermeNunes,它是从 ReactJS 组件发送的。但问题不是客户端,而是 Rails 在做
magic。
标签: ruby-on-rails json ruby strong-parameters