Rails 3.2 `link_to` (in email) with `method: :put` still producing GET request

后端 未结 2 1943
情歌与酒
情歌与酒 2020-12-11 09:44

In my app I have automated e-mails reminding applications to complete the next step in the interview process. The e-mail has an opt out link which, when clicked, should hit

相关标签:
2条回答
  • 2020-12-11 10:30

    It's because this is a link in context of an email. It's documented behavior:

    method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If href: '#' is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, patch?, or put?.

    There are security reasons to disallow executable JavaScript in emails, as well as forms. Therefore, it's impossible to render a form with JS and execute a PUT request, so this falls back to GET as stated in citation above.

    0 讨论(0)
  • 2020-12-11 10:33

    The method option on link_to works because your app includes some javascript that intercepts clicks on the link. That javascript creates a form with the correct method (adding an input element for request methods browsers don't support) and supports that.

    This won't work in an email: your site's javascript isn't present and email clients don't usually run javascript.

    Including an actual form works in some cases, but very frequently doesn't (or displays scary warnings) - see this question for example.

    There's not much alternative to just having a plain "get" link unfortunately (you could of course have that go to a confirmation page where you have a button/link that will fire a post request).

    0 讨论(0)
提交回复
热议问题