我在rails 4中使用一个布尔复选框对服务条款进行验证,我看到params正在通过,但我的验证仍然会抛出一个错误。
html.erb
<%= check_box_tag(:agree) %>
<%= label_tag(:agree, "I Agree") %>
devise_permitted_parameters.rb
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :agree
devise_parameter_sanitizer.for(:account_update) << :first_name << :last_name
end
如果我在模型中这样做是行不通的:
validates_acceptance_of :agree, :allow_nil => false, :accept => true, :on => :create
服务器:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VfMsm8nigtdMDh/jo6SyU8mUt5LIWlz0ymuiJwl4qFY=",
"user"=>{"first_name"=>"c", "last_name"=>"c", "email"=>"abc@def.com", "password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"}, "agree"=>"1", "commit"=>"Register"}
Unpermitted parameters: first_name, last_name
不知道为什么我有不允许的对撞机..。只有当我玩弄提交表格时,它才会出现。
如果我使用来自其他帖子的这个建议,它会通过,但如果未选中“同意”框,它也允许注册:
validates_inclusion_of :agree, :in => [true, false]
发布于 2014-09-04 09:04:01
在查看了代码之后,我意识到使用了form_for,这就是为什么form_tag语法不能像预期的那样工作的原因。它还显示了为什么它仍然输出提交的信息。
我仍然不能百分之百确定为什么名字和姓氏是不允许的参数。我认为这是解决各种问题的cookie问题,如果您重新启动服务器,就会再次恢复正常。
希望这能在未来节省别人的时间。
https://stackoverflow.com/questions/25654364
复制