我正在尝试使用JSON通过POST创建设计用户。
控制器的当前代码是
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":"test@email.com","username":"test"}, "auth_token":"my token"}
问题是我看到一个错误,需要将一些似乎是Rails Internal的属性列入白名单。我没有user, format, action, controller
作为我的用户模型的属性。我尝试在User模型中向它们添加attr_accesible
,但然后POST返回找不到这些属性。
<h1>
ActiveModel::MassAssignmentSecurity::Error
in V1::UsersController#create
</h1>
<pre>Can't mass-assign protected attributes: user, format, action, controller</pre>
知道如何解决这个问题吗?
答案 0 :(得分:4)
你必须引用params [:user]而不是params:
@user = User.create(params[:user])