【问题标题】:Strong parameters with has_many gives "no implicit conversion of Symbol into Integer"has_many 的强参数给出“没有将符号隐式转换为整数”
【发布时间】:2013-09-20 13:43:45
【问题描述】:

尝试从 json 请求创建用户,但我的服务器给了我“typeError(没有将 Symbol 隐式转换为 Integer)”。我知道我的嵌套属性有问题,但我不知道是什么,这让我发疯了..

我的 Javascript 文件:

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"=>"foo@example.com", "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”。谢谢!

更新

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

【问题讨论】:

  • 你能发布你的创建方法代码吗?
  • 我已经更新了我的帖子。
  • 嗯,如果我移动“authentications_attributes: [:id, :user_id, :provider, :uid]”首先它会给我错误“SyntaxError (../users_controller.rb:41: syntax error , 出乎意料的 ',', 期待 => ...user_id, :provider, :uid], :id,.." 在 "," 在 :id 之后。是对还是?
  • 我认为你的哈希有问题

标签: json ruby-on-rails-4 strong-parameters


【解决方案1】:

我的哈希值错误,正确的哈希值应该是:

user = {
    ....
    authentications_attributes: [
        {
            provider: @get('provider')
            uid: @get('uid')
        }
    ]
}

【讨论】:

    【解决方案2】:

    请试试这个,如果它不起作用,请告诉我。

    def new
      @user = User.new
      @user.authentications.build
    end
    
    def create
      @user = User.new(user_params)
    
      respond_to do |format|
        if @user.save
          format.json { user: { id: @user.id, auth_token: @user.session_api_key } }, status: 201
        else
          format.json { errors: @user.errors.messages }, status: :unprocessable_entity
        end
      end
    end
    
    private
    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :registration_completed, authentications_attributes: [:provider, :uid])
    end
    

    【讨论】:

      猜你喜欢
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      相关资源
      最近更新 更多