2012-09-05 63 views
1

我試圖通過POST使用JSON創建設計用戶。mass-assign受保護的屬性:用戶,格式,操作,控制器

控制器的當前的代碼是

class V1::UsersController < ApplicationController 
    load_and_authorize_resource 
    respond_to :json 

    def create 
    @user = User.create(params) 
    if @user.save 
     render json: @user, status: :created 
    else 
     render json: @user.errors, status: :unprocesssable_entry 
    end 
    end 

end 

我送以下JSON作爲POST到http://localhost:3000/v1/users.json

{"user":{"active":true,"email":"[email protected]","username":"test"}, "auth_token":"my token"} 

的問題是,我看到一個錯誤要求到白名單一些屬性似乎是Rails Internal。我沒有user, format, action, controller作爲我的用戶模型的屬性。我試圖在用戶模型中爲它​​們添加attr_accesible,但是POST返回那些屬性沒有找到。

<h1> 
    ActiveModel::MassAssignmentSecurity::Error 
    in V1::UsersController#create 
</h1> 
<pre>Can&#x27;t mass-assign protected attributes: user, format, action, controller</pre> 

任何想法如何解決這個問題?

回答

4

您必須參考PARAMS [:用戶]不PARAMS:

@user = User.create(params[:user]) 
相關問題