Timmie Sarjanen
2013-Sep-22 17:24 UTC
[Rails] Strong parameters with has_many gives “no implicit conversion of Symbol into 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.. 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: name, :last_name, :email, :password, :password_confirmation, :registration_completed, authentications_attributes: [:id, :user_id, :provider, :uid]) Console: Started POST "/api/users" for 127.0.0.1 at 2013-09-20 15:39:49 +0200Processing by Api::UsersController#create as */* Parameters: {"user"=>{"email"=>"foo-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org", "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'' Create method: 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 What am I doing wrong? And of course have i "accepts_nested_attributes_for :authentications" in my User.rb file. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5e59e147-4987-4e99-87b3-0ec5d05e2d83%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Sep-23 15:51 UTC
[Rails] Re: Strong parameters with has_many gives “no implicit conversion of Symbol into Integer”
On Sunday, September 22, 2013 6:24:47 PM UTC+1, Timmie Sarjanen wrote:> > 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.. >in general that errors means that something is trying to use an array as a hash, ie passing a non integer key to []> Processing by Api::UsersController#create as */* > Parameters: {"user"=>{"email"=>"f...-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org <javascript:>", "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 > > Those parameters aren''t right - authentications_attributes should be ofthe form [ {''provider'' => ''facebook'', ...} ] or (since you can''t really do arrays of hashes when using regular forms { ''123456'' => {''provider'' => ''facebook'', ...}, ''456789'' => {''provider'' => ''google'', ...} } The keys in that hash are arbitrary. It''s common to use the current time in milliseconds - anything that is unique is good enough Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0871716f-7e4c-43c8-8e30-67625844cd72%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.