简体   繁体   English

Ruby on Rails link_to with put 方法

[英]Ruby on Rails link_to With put Method

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.我是 Rails 新手,我正在尝试使用 link_to 助手创建一个链接,该链接发出 PUT 请求而不是 GET 请求。 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 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=put with the source code <a href="/admin/users/7/activate?method=put">Activate</a> .但是,这会生成 URL(例如) /admin/users/7/activate?method=put和源代码<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.我意识到我可以使用 button_to,但我一直在努力解决这个问题,我很困惑为什么我会看到这种行为,而其他教程说我所做的应该是有效的。 How can I go about creating a link_to helper with the behavior I want?我如何着手创建一个具有我想要的行为的 link_to 助手?

Updated - The link_to helper will do a GET unless a method is specified.更新- 除非指定了方法,否则link_to助手将执行 GET。

Its better specifying the exact request type, instead of match in your routes file.最好指定确切的请求类型,而不是在您的路由文件中match How about replacing match by put in routes as :如何通过put路由替换match

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

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

The activate_user method should reside in admins controller. activate_user方法应该驻留在admins控制器中。 The docs has more info on link_to helper. 文档有关于link_to助手的更多信息。

link_to thinks that :method => :put is part of the path hash. link_to认为:method => :put是路径哈希的一部分。 You have to tell it otherwise.否则你必须告诉它。 Wrap your path in brackets.将您的路径括在括号中。

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

Now link_to will recognize :method => :put as 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.保持事物整洁,并避免像这样挑剔的情况。

If You are using link_to do then you can use the following syntax如果您使用link_to do 那么您可以使用以下语法

<%= link_to admin_subscription_path(user), method: :put do %>
 # can put html & css here
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM