Ruby on Rails link_to with put 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9305753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:50:40  来源:igfitidea点击:

Ruby on Rails link_to With put Method

ruby-on-railslink-to

提问by maksim

I'm new to Rails, and I'm trying to use the link_to helper to create a link that issues a PUT request instead of a GET request. Specifically, I'm trying to create a link that activates a user's account in my app from the admin's panel. I'm using Rails 3.0.5.

我是 Rails 新手,我正在尝试使用 link_to 助手创建一个链接,该链接发出 PUT 请求而不是 GET 请求。具体来说,我正在尝试创建一个链接,用于从管理面板激活我的应用程序中的用户帐户。我正在使用 Rails 3.0.5。

My routes.rb file has:

我的 routes.rb 文件有:

match '/admin/users/:id/activate' => 'admin#activate_user',
  :action => :activate_user, :via => :put

My view has:

我的观点有:

link_to 'Activate', :action => :activate_user, :id => user.id, :method => :put

However this generates the URL (for example) /admin/users/7/activate?method=putwith the source code <a href="/admin/users/7/activate?method=put">Activate</a>.

但是,这会生成/admin/users/7/activate?method=put带有源代码的 URL(例如)<a href="/admin/users/7/activate?method=put">Activate</a>

I'd like to generate, instead, <a href = "/admin/users/7/activate" data-method="put">Activate</a>

我想生成,而不是, <a href = "/admin/users/7/activate" data-method="put">Activate</a>

I realize I could use button_to, but I've been wrestling with this issue for a while and I'm confused why I'm seeing this behavior, when other tutorials say that what I'm doing should be valid. How can I go about creating a link_to helper with the behavior I want?

我意识到我可以使用 button_to,但我一直在努力解决这个问题,我很困惑为什么我会看到这种行为,而其他教程说我正在做的事情应该是有效的。我如何着手创建一个具有我想要的行为的 link_to 助手?

回答by prasvin

Updated- The link_tohelper will do a GET unless a method is specified.

更新-link_to除非指定了方法,否则帮助程序将执行 GET。

Its better specifying the exact request type, instead of matchin your routes file. How about replacing matchby putin routes as :

最好指定确切的请求类型,而不是match在您的路由文件中。如何更换match通过put在路线为:

put '/admin/users/:id/activate' => 'admins#activate_user', :as => 'activate_user'

link_to 'Activate', activate_user_path(user.id), method: :put

The activate_usermethod should reside in adminscontroller. The docshas more info on link_tohelper.

activate_user方法应驻留在admins控制器中。该文档有更多信息link_to的帮手。

回答by Substantial

link_tothinks that :method => :putis part of the path hash. You have to tell it otherwise. Wrap your path in brackets.

link_to认为这:method => :put是路径哈希的一部分。否则你必须告诉它。将您的路径括在括号中。

link_to 'Activate', {:action => :activate_user, :id => user.id}, :method => :put

Now link_towill recognize :method => :putas an option, not part of the link's path.

现在link_to将识别:method => :put为一个选项,而不是链接路径的一部分。

As a side note, you should try to use route helpers instead of path hashes whenever possible. Keeps things nice and tidy, and avoids nit-picky situations like this.

作为旁注,您应该尽可能尝试使用路由助手而不是路径哈希。保持事物整洁,并避免像这样挑剔的情况。