【问题标题】:rails json - validates acceptance ofrails json - 验证接受
【发布时间】:2016-12-07 01:11:05
【问题描述】:

我有一个 Rails 应用程序,它制作了一个离子(角度)应用程序使用的 API。在我的用户注册中,我要求用户在注册前同意服务条款,并将其设置在我控制器的参数中。

模型面是这样的:

# agreeing to terms of service
  validates_acceptance_of :terms_of_service, on: :create

控制器如下所示:

def mobile_user_params
    allowed = [:first_name, :last_name, :email, :password, :password_confirmation, :location, :terms_of_service]
    params.require(:user).permit(allowed)
end

参数中的最终结果如下所示:

{"user"=>{"terms_of_service"=>true}, "controller"=>"users", "action"=>"mobile_create", "format"=>"json"}

我的问题是什么,即使它返回 {"terms_of_service"=>true} 因为我接受它,这也会返回一条错误消息:

errors: {"Terms of Service must be accepted"}

有谁知道我怎样才能满足这个验证?它在使用复选框呈现 HTML 时有效,但在 JSON 请求上我似乎无法满足它。

非常感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails angularjs json


    【解决方案1】:

    你看过doc吗?

    根据:

    :accept - 指定被视为接受的值。默认值为字符串“1”,这使得与 HTML 复选框相关联变得容易。如果您正在验证数据库列,则应将其设置为 true,因为该属性在验证之前从“1”类型转换为 true。

    我认为你应该添加:validates_acceptance_of :terms_of_service, accept: true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多