gpt4 book ai didi

ruby-on-rails - Rails 4 - 使用设计创建新用户注册时复选框协议(protocol)验证失败

转载 作者:行者123 更新时间:2023-12-04 05:31:01 25 4
gpt4 key购买 nike

我正在尝试在表单上包含一个“接受服务条款”复选框(称为 agreement )。该应用程序正在使用 Devise用于用户管理。
views/devise/registrations/new.html.erb拥有:

<%= f.check_box :agreement, class: 'form-control' %>
application_controller.rb拥有:
  def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:agreement, :phone, :first_name, :last_name, :domain, :email, :password, :password_confirmation) }
end

然后在 user.rb Controller ,它有
# neither of the below worked
# validates :agreement, :acceptance => true
validates :agreement, acceptance: true

如果我查看 development.log 中的数据,它显示 agreement字段正确通过,值为 1,即(根据 Rails docs ,验证的预期值):
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jFg6+ZDM1qldh020lv/FQHxlgZkby2dhUbejjXurr4w=", "user"=>{"first_name"=>"Joe", "last_name"=>"Smith", "phone"=>"2098993344", "domain"=>"google.com", "email"=>"test@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "agreement"=>"1"}, "commit"=>"Create User"} 

但是,无论何时提交表单,它都会显示“必须接受协议(protocol)”的错误消息,无论是否选中。

关于这是由什么引起的任何想法?

最佳答案

尝试使用它进行验证
validates :agreement, acceptance: { accept: true }
根据 Rails 文档

:accept - Specifies value that is considered accepted. The default value is a string “1”, which makes it easy to relate to an HTML checkbox. This should be set to true if you are validating a database column, since the attribute is typecast from “1” to true before validation.

关于ruby-on-rails - Rails 4 - 使用设计创建新用户注册时复选框协议(protocol)验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com