2013-07-13 44 views
13

我试图通过JSON注册有一项用户,但不断收到ActiveModel::ForbiddenAttributesError轨道4和设计 - 通过JSON API用户注册

class Api::V1::RegistrationsController < ApplicationController 

    skip_before_filter :verify_authenticity_token 
    respond_to :json 

    def create 

    user = User.new(params[:user]) 

    if user.save 
     render :json => user.as_json(:auth_token=>user.authentication_token, :email=>user.email), :status=>201 
     return 
    else 
     warden.custom_failure! 
     render :json => user.errors, :status=>422 
    end 
    end 

    def user_params 
    params.require(:user).permit(:email, :password) 
    end 

end 

这里是我的JSON请求:

Processing by Api::V1::RegistrationsController#create as JSON 
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}} 

我知道这与强参数有关,但尚未能破解它。

回答

11

我会改变:

user = User.new(params[:user]) 

有:

user = User.new(user_params) 

从文档:

# This will raise an ActiveModel::ForbiddenAttributes exception because it's using mass assignment 
# without an explicit permit step. 
def create 
    Person.create(params[:person]) 
end