rails link_to using get instead of post
Asked Answered
M

4

5

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
Merrymerryandrew answered 7/12, 2015 at 15:56 Comment(3)
Have you added this path in routes.rb? I think the link which you are trying to access is: /relationships/:friend_idDenyse
please post your routes.rb fileBlain
I've added what is in my routes file.Merrymerryandrew
F
9

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.

Frizz answered 8/12, 2015 at 14:54 Comment(4)
jquery-ujs is in my gemfile and it has been installed. I'm not sure how to my asset pipline. I've also tried using a button, but I get the same error.Merrymerryandrew
@B.Allred It would help to see the full HTML for the page you're clicking from, and bring up the JavaScript console in your browser for the page and see if there are any errors there.Frizz
I've gotten it to work now. At an instructor's recommendation, I changed the button's code to this: <%= button_to "subscribe", {action: 'create', controller: 'subscriptions', interest_id: @interest.id } %> . Thank you for your advice!Merrymerryandrew
Thank you! I hadn't understood that javascript was closing the gap on post requests on link_to using method:. I had not started my webpacker so simply spinninng up another console and manually running ./bin/webpack-dev-server and restarting rails server got all my links working again.Bemis
S
1

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 } %>
Sextan answered 2/7 at 18:21 Comment(1)
perfect. thanks -- data: { turbo_method: :post } works instead of method: postZoraidazorana
D
0

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

Delafuente answered 7/12, 2015 at 16:48 Comment(2)
Here's what comes up when I run rake routes: relationships GET /relationships(.:format) relationships#index POST /relationships(.:format) relationships#createMerrymerryandrew
That is odd, what happens if you try with this? <%= button_to "Add as Friend", relationships_path(friend: @user) %> Or if you write a new route like post 'test', to: 'relationships#create' (you shouldn do this but just for testing purposes)Delafuente
P
0

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

Peggie answered 9/11, 2020 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.