如何使jQuery Post匹配rails控制器帖子

时间:2016-09-27 17:18:34

标签: javascript jquery ruby-on-rails

我试图写一个jquery帖子。

如果我通过标准的rails控制器和表单来完成,这就是成功的帖子:

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"}

使用jQuery,这是我的帖子请求:

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

但这是结果:

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'

问题是我没有将这些参数传递给名为languages_user的数组吗?有没有办法将所有这些参数分配给languages_user?任何有关为什么这篇文章会以缺失的参数回来的见解将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

您还需要主键languages_user,试试这个:

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