2

So I am trying to use the following route:

<%= link_to like_post_path(@post), :method => :put do %>

But I don't know why is using GET method instead of PUT

 No route matches [GET] "/posts/1/like"

makes no sense to me..

myroutes.rb:

 resources :posts do
    member do
        put "like" => "posts#upvote"
        put "dislike" => "posts#downvote"
    end
Sam Tyson
  • 4,496
  • 4
  • 26
  • 34
Ezequiel Ramiro
  • 760
  • 1
  • 12
  • 29

2 Answers2

4

You are using correct format and your code is should generate something like this:

<a data-method="put" href="...">

From your routes error message we can conclude that it is not being sent using POST with _method=put params. So, the problem must be that you did not include jQuery and Rails jQuery extension javascript files.

An easy fix would be to include application.js file (jquery and rails js extension are included by default) in your page.

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • 1
    This is a great suggestion, make sure data-method="put" is set, and makes ure you are requering `jquery_ujs` in your *application.js*. – Leonel Galán May 25 '16 at 17:38
  • That was it!! Thank you so much. The problem was my Jquery and Rails Query due to http://stackoverflow.com/questions/28421547/rails-execjsprogramerror-in-pageshome – Ezequiel Ramiro May 25 '16 at 17:59
0

link_to signature :

link_to(name = nil, options = nil, html_options = nil, &block)

According to the doc, :method belongs to the options hash.

calling

<%= link_to like_post_path(@post), :method => :put do %>

results in putting into

  • name : like_post_path(@post),
  • options: nil,
  • html_options: { method: :put }

From Hash

Hashes are also commonly used as a way to have named parameters in functions. Note that no brackets are used below. If a hash is the last argument on a method call, no braces are needed, thus creating a really clean interface:

EDIT

<%= link_to like_post_path(@post), { method: :put } do %>

leads to what you are trying to achieve.

floum
  • 1,149
  • 6
  • 17