2

The other solutions here did not work, so I am posting a new question. I am using acts_as_votable for up and down voting on links, but when I try to actually vote I get the error:

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

This is what my view looks like:

<%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
        <span class="glyphicon glyphicon-chevron-up"></span>
        Upvote
        <%= link.get_upvotes.size %>
      <% end %>
      <%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
        <span class="glyphicon glyphicon-chevron-down">
        Downvote
        <%= link.get_downvotes.size %>
      <% end %>

And this is what my routes look like:

  resources :links do
    member do
      put "like", to: "links#upvote"
      put "dislike", to: "links#downvote"
    end
  end

I see that my client is trying to access the get method, but I explicitly call method: :put in my index.html.erb file.

Do you know why it is trying to access get and how I can override that?

Jbur43
  • 1,284
  • 17
  • 38

2 Answers2

2

This problem is almost always to do with JQuery, although in this instance I'm not sure why.

You can even see it here:

Note that if the user has JavaScript disabled, the request will fall back to using GET

I would firstly check that you have JQuery in your app, and then that it's getting called okay. If it isn't loading, then the best course of action is to see why JQuery is not being called (it's either going to because it won't be referenced, or you'll have an error preventing it from loading).

Your code actually looks valid, so I'd have to say that JQuery would be the likely issue here.


You could polish up your routes etc:

#config/routes.rb
resources :links do
   match "vote", action: :vote, via: [:put,:delete], on: :member
end

This will send both types of request to an action called vote, which you can then split up as follows:

#app/controllers/links_controller.rb
class LinksController < ApplicationController
   def vote
      if request.put?
         # Code to vote
      elsif request.delete?
         # Code to downvote
      end
      # Redirect back
   end
end

Much DRYer and more efficient (only calling a single action etc)

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Do the downvote and upvote actions both need to be put requests though? I'm just wondering how it works for my own understanding, I'm having some trouble getting the action to work. – Jbur43 Dec 16 '15 at 23:14
  • If you're defining the actions in your own controller, you can use whichever routes you like. I use the routes I provided in the apps I work on, because it's a single action which "toggles" the vote. You can use whichever method you want. – Richard Peck Dec 17 '15 at 08:40
1

It turns out that the answer was related to JQuery. I was getting an Uncaught ReferenceError: jQuery is not defined error and so what I did was include JQuery outright in my application.html.erb file.

You have to put this ahead of all your other scripts.

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

After that, the voting began to work

Jbur43
  • 1,284
  • 17
  • 38