Rails3 button_to 正在调用 POST 操作,尝试调用 PUT 操作 [英] Rails3 button_to is calling POST action, trying to call PUT action

查看:54
本文介绍了Rails3 button_to 正在调用 POST 操作,尝试调用 PUT 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要执行 PUT 操作的 button_to(关于此资源只有一件事可以更新 - 它将被更新为已确认",因此没有其他表单字段与触发行动).

I have a button_to that I want to perform a PUT action (there is only one thing that can be updated about this resource - it will be updated as being 'acknowledged', so there are no other form fields associated with firing the action).

这是在我看来(控制器是明确给出的,因为按钮位于属于另一个控制器的视图上):

This is in my view (the controller is given explicitly because the button is on a view that belongs to another controller):

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

在我的路由文件中,该资源已被声明为静态资源:

In my routes file, the resource has been declared as a restful resource:

  resources :practice_sessions

这个资源的控制器有一个创建和更新动作,上面的button_to调用了创建动作.我希望它调用更新操作.

The controller for this resource has a create and an update action, and the button_to above calls the create action. I want it to call the update action.

这是在创建操作触发之前通过日志显示的:

This comes through the log right before the create action fires:

Started POST "/practice_sessions?id=21&method=put" for 127.0.0.1 at 2010-11-17 08:52:46 +0000
  Processing by PracticeSessionsController#create as HTML
  Parameters: {"authenticity_token"=>"1EW0IlI38d0f4wST5azrCEZVZPfih7i0UvCGSF7eqbc=", "id"=>"21", "method"=>"put"}

推荐答案

您的语法略有偏差.button_to 接受三个参数:按钮标题、options 哈希值和 html_options 哈希值.:method=>:put 需要放在 html_options 中,而路由参数需要放在 options 中.所以你可以像这样重写:

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 %>

当点击请求应该由 PracticeSessionsController#update

这篇关于Rails3 button_to 正在调用 POST 操作,尝试调用 PUT 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆