本文介绍了具有has_many的强参数给出“没有将符号隐式转换为整数"的信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试从json请求创建用户,但是我的服务器给我"typeError(没有将Symbol隐式转换为Integer)"的提示.我知道嵌套属性有问题,但是我不知道是什么,这使我发疯.
Trying to create a user from a json request but my server gives me "typeError (no implicit conversion of Symbol into Integer)". I understand that it's something wrong with my nested attribute but i dont know what, this's driving me crazy..
我的Javascript文件:
My Javascript file:
user = {
email: @get('email')
first_name: @get('firstName')
last_name: @get('lastName')
password: @get('password')
password_confirmation: @get('passwordConfirmation')
registration_completed: true
authentications_attributes: {
provider: @get('provider')
uid: @get('uid')
}
}
$.post("/api/users", { user })
参数:
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :registration_completed, authentications_attributes: [:id, :user_id, :provider, :uid])
控制台:
Started POST "/api/users" for 127.0.0.1 at 2013-09-20 15:39:49 +0200
Processing by Api::UsersController#create as */*
Parameters: {"user"=>{"email"=>"[email protected]", "first_name"=>"Foo", "last_name"=>"Bar", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration_completed"=>"true", "authentications_attributes"=>{"provider"=>"facebook", "uid"=>"10000000"}}}
Completed 500 Internal Server Error in 97ms
TypeError (no implicit conversion of Symbol into Integer):
app/controllers/api/users_controller.rb:17:in `create'
我做错了什么?当然,我的User.rb文件中也有"accepts_nested_attributes_for:authentications".谢谢!
What am I doing wrong? And of course have i "accepts_nested_attributes_for :authentications" in my User.rb file. Thanks!
更新
def create
@user = User.new(user_params)
@user.authentications.build
authorize! :create, @user
if @user.save
render json: { user: { id: @user.id, auth_token: @user.session_api_key } }, status: 201
else
render json: { errors: @user.errors.messages }, status: :unprocessable_entity
end
end
推荐答案
我的哈希错误,正确的哈希应该是:
My hash was wrong, right hash should be:
user = {
....
authentications_attributes: [
{
provider: @get('provider')
uid: @get('uid')
}
]
}
这篇关于具有has_many的强参数给出“没有将符号隐式转换为整数"的信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!