Rails 3.2 'Link_To' (In Email) with 'Method: :Put' Still Producing Get Request

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

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.

Link works fine in rails view, but not in email

The link is being created correctly, but rails unobtrusive javascript handles actually making the link do a put request. Since this javascript doesn't exist in the email, the link will only be able to make a get request.

This email link would more usually link to the edit action (with a get), not actually perform a change directly.

Using Rails link_to for links that post

The short answer is that if what you mean by "parameters" is form fields, then you simply can't do this (at least not in a straightforward way that I can see). You should instead use a form with a submit button, styled to look like a link (if that's what you want it to look like).

If on the other hand you had meant query parameters, then this would work:

link_to "Profile", profile_path(@profile.id, param1: 'value1', param2: 'value2'), method: :post

Rails' link_to method: GETing when it should DELETE

Most browsers don't actually support the DELETE verb, so Rails fakes it by modifying the HTML it generates. Rails tacks on a HTML5 attribute called data-method and sets it to "delete". So when a user clicks on the link, it is actually issued as a GET request, but the data-method attribute allows for some Rails magic and means your routing code should recognize it as a DELETE request.

edit:

You can test it yourself in the console. Run bundle exec rails c to get into the console, and look at the HTML that this generates:

helper.link_to "delete", "foobar/delete", :method => 'delete'

The HTML should look like this:

<a href="foobar/delete" data-method="delete" rel="nofollow">delete</a>

Custom action not firing using link_to with remote=true

The issue was the :url symbol. I changed:

<%= link_to "Flag User", :url => flag_user_path(user.id), :method => :get, :remote => true %>

to:

<%= link_to "Flag User", flag_user_path(user.id), :method => :get, :remote => true %>

and everything works was expected. I was incorrectly using it like link_to_remote which requires the :url symbol. Thanks everyone for their input.

Ruby on Rails link_to With put Method

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

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.

button_to using GET method

As FancyDancy says, it's still a post request even though the information gets passed through the URL. Rails doesn't make a distinction between $_GET and $_POST. It only has the equivalent of $_REQUEST: params. So it doesn't really matter if the product_id gets passed via the URL or via a hidden form field. In both cases you can get it via params[:product_id].



Related Topics



Leave a reply



Submit