Rails3 button_to is calling POST action, trying to call PUT action

半腔热情 提交于 2019-12-04 03:47:15

Your syntax is slightly off. button_to takes three arguments: the button title, an options hash, and an html_options hash. :method=>:put needs to go in html_options, while the route parameters need to go in options. So you can rewrite like so:

<%= button_to "Acknowledged", { :controller => 'practice_sessions',
  :id => @practice_session.id}, 
  :method => :put %>

When clicked the request should be handled by PracticeSessionsController#update

In the end I decided to go with the more restful approach using named routes, which seems to work fine. I'm still not 100% sure why the other method wouldn't work, but I don't think it matters because this seems both a) more tidy and b) more conventional.

<%= button_to "Acknowledge", practice_session_path(@practice_session), :method => :put %>

You might need to explicitly pass the :method => :put argument in the html_options hash - it might be getting globbed into the options hash.

Try this:

<%= button_to "Acknowledged", { :controller => 'practice_sessions', :id => @practice_session.id }, :method => :put %>

(Note the explicit braces around :controller and :id)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!