Ruby on Rails link_to with put Method

时间:2022-09-21 00:17:29

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=put with the source code <a href="/admin/users/7/activate?method=put">Activate</a>.

但是,这会生成URL(例如)/ admin / users / 7 / activate?method = put,源代码为激活

I'd like to generate, instead, <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帮助器?

2 个解决方案

#1


28  

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. How about replacing match by put in routes as :

它更好地指定确切的请求类型,而不是匹配您的路由文件。如何通过放入路线替换匹配:

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. The docs has more info on link_to helper.

activate_user方法应驻留在管理员控制器中。 docs有更多关于link_to帮助器的信息。

#2


31  

link_to thinks that :method => :put is 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_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.

作为旁注,您应尽可能尝试使用路径助手而不是路径哈希。让事情变得干净整洁,避免这种挑剔的情况。

#1


28  

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. How about replacing match by put in routes as :

它更好地指定确切的请求类型,而不是匹配您的路由文件。如何通过放入路线替换匹配:

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. The docs has more info on link_to helper.

activate_user方法应驻留在管理员控制器中。 docs有更多关于link_to帮助器的信息。

#2


31  

link_to thinks that :method => :put is 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_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.

作为旁注,您应尽可能尝试使用路径助手而不是路径哈希。让事情变得干净整洁,避免这种挑剔的情况。