B. Allred
B. Allred

Reputation: 439

rails link_to using get instead of post

I'm making a website for a class and I'm trying to implement a friend request function with a model called 'Users' and a join model called 'Relationships'. I have a button on the user#show page that should add a friend by using the create method in the Relationships controller. Here is the code for the button:

<%= link_to "Add as Friend", relationships_path(:friend_id => @user), method: :post %>

When I press the link, however, it tries to access the index method instead. After looking in the console, it looks like the link is sending a GET request, which routes to the index method, instead of a POST request, which routes to the create method. Can someone explain why this error is occurring and how I can fix it?

Edit: As requested, here is what I have in my routes:

Rails.application.routes.draw do
  resources :interests
  get 'interests/create'

  get 'interests/destroy'

  get 'home/index'

  get 'sessions/create'

  get 'sessions/destroy'

  resources :users
  resources :relationships
  resources :subscriptions

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'
  root 'home#index'
  get "/auth/:provider/callback" => "sessions#create"
  get "/signout" => "sessions#destroy", :as => :signout

Upvotes: 5

Views: 4173

Answers (4)

Zohaib Riaz
Zohaib Riaz

Reputation: 21

I had the same issue.

<%= link_to 'Submit', submit_purchase_order_path(@purchase_order), method: :post %>

Solution: If you are using rails 7.

rails importmap:install
rails turbo:install 
rails stimulus:install

Configure import map in config/importmap.rb

import "@hotwired/turbo-rails"

and updated the link_to:

<%= link_to 'Submit', submit_purchase_order_path(@purchase_order), data: { turbo_method: :post } %>

Upvotes: 1

nflauria
nflauria

Reputation: 897

Using 'url' instead of 'path' with the route helper solved the problem for me. So instead of 'relationships_path' use 'relationships_url'.

Upvotes: 0

Jim Van Fleet
Jim Van Fleet

Reputation: 1118

Using a link_to helper indicates to Rails that you'd like to produce an a tag in your HTML. No element of the HTML specification regarding a tags allows for producing POST requests. Because Rails understands the utility of allowing for POST and DELETE requests to be issued using links, however, it provides those options in the link_to helper. It's implementation, though, must use JavaScript under the hood in order to appropriately function.

Check that jquery-ujs is installed, and that your asset pipeline is working correctly in order to use the helper in this way.

You may also evaluate whether using a form_for and a button is better, since that will automatically POST.

Upvotes: 9

alebian
alebian

Reputation: 782

I'm pretty sure you are matching the wrong route. Run rake routes and see the route that links to the Relationships#create.

Upvotes: 0

Related Questions