Using Ruby 2.5.1/Rails 5.2.4.1 I generated a new Rails app, and edited/created the following files:
# config/routes.rb
Rails.application.routes.draw do
root 'some#home'
post 'foo' => 'some#foo'
end
# app/controllers/some_controller.rb
class SomeController < ApplicationController
def foo; end
end
# app/views/some/home.html.erb
<%= link_to 'Do stuff', foo_path, method: :post %>
If I click this link normally, it makes a POST request and gives a success response (204), as expected.
If I click to open it in a new tab (middle click/right click and 'Open link in new tab'), it makes a GET request and gives a 404/RoutingError.
This different behaviour depending on how I open (what appears by default to the user to be) a normal link seems surprising - is this the expected behaviour? I'm guessing this is something to do with this link using the Rails UJS and this behaving differently when a new tab is opened? What's the best way in Rails to get (what looks like) a link that makes a POST, no matter how it's opened?
right-click and 'Open link in new tab' is just like copying the link and pasting in a new tab. Therefore it acts as a get-method.
Since you only accept post-method, RoutingError occurs.
btw, you can use target='_blank' if you want to open post-link in a new tab.