2013-07-13 32 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