2016-12-07 56 views
0

的接受程度我有一个导航应用程序,它使离子(角度)应用程序消耗的API成为可能。在我的用户注册中,我要求用户在注册前同意服务条款,并在我的控制器中将其设置在我的参数中。rails json - 验证

在模型方面是这样的:

# 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 

和最终结果在PARAMS是这样的:

{"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请求上我似乎无法满足它。

任何帮助都将非常感激。

回答

0

你看看doc吗?

据:

:接受 - 指定被认为是公认的价值。默认值是一个字符串“1”,这可以很容易地与HTML复选框相关联。如果您正在验证数据库列,则应该将其设置为true,因为该属性在验证之前从“1”变为true。

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