gwalshington gwalshington - 2 months ago 14
Javascript Question

How to make jQuery Post to match rails controller post

I'm trying to write a jquery post.

If I do it through the standard rails controller and form, this is the successful post:

Started POST "/languages_users" for 127.0.0.1 at 2016-09-27 12:45:16 -0400

Processing by LanguagesUsersController#create as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"flRhWjgP3p4sUpFkZUurIr3CJBITQ+j+2FKQYNHwN4t8Mo5rh+UCPvhPgu0ORKVioK+JvYsHInSkHUofm7iyqA==", "languages_user"=>{"language_id"=>"1", "level"=>"5"}, "commit"=>"Save Native Language"}


With jQuery, this is my post request:

$.post( "/languages_users", { language_id: confirmedUserNativeInt, user_id: userId, level: 5 })


But this is the result:

Started POST "/languages_users" for 127.0.0.1 at 2016-09-27 12:46:27 -0400
Processing by LanguagesUsersController#create as */*
Parameters: {"language_id"=>"2", "user_id"=>"1", "level"=>"5"}


ActionController::ParameterMissing (param is missing or the value is empty: languages_user):
app/controllers/languages_users_controller.rb:123:in `languages_user_params'
app/controllers/languages_users_controller.rb:71:in `create'


Is the issue that I'm not passing those params as an array called languages_user? Is there a way to assign all those params to languages_user? Any insight into why this post is coming back with a missing param would be appreciated.

Thanks!

Answer

You need also main key languages_user, try this:

 $.post( "/languages_users", { languages_user:{ language_id: confirmedUserNativeInt, user_id: userId, level: 5 } })