0

I am implementing a like system in my rails app using David Celis gem called Recommendable. I've gotten everything to work in the console but I can't get the right routes and I'm getting the "No route matches [GET] "/categories/1/posts/1/like" error.

I have the following in my models:

class Category < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
  extend FriendlyId
  friendly_id :name, use: :slugged
end

class Post < ActiveRecord::Base
  belongs_to :category
end

In my Post Controller I have:

class PostsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :get_category
  def like
    @post = Post.find(params[:id])
    respond_to do |format|
      if current_user.like @post
      else
         flash[:error] = "Something went wrong! Please try again."
         redirect_to show_post_path(@category, @post)
      end
    end
  end
end

In my routes I have:

resources :categories do
    resources :posts do
      put :like, :on => :member
    end
end
match 'categories/:category_id/posts/:id', :to => 'posts#show', :as => 'show_post'

Can someone please point at my errors? I can get the PUT to work but I dont know where the GET error is coming from as I am trying to redirect back to the post if an error occurs when the user like's a certain post. Thank you in advance.

EDIT:

In my view I have:

- title "#{@post.class}"
%p#notice= notice

%p
  %b Title:
  = @post.title
%p
  %b Description:
  = @post.description
%p
  %b Likes:
  = @post.liked_by.count

= link_to 'Edit', edit_category_post_path(@post)
\|
= link_to 'Back', category_posts_path
\|
= link_to 'Like', like_category_post_path(@post)
Daniel Garzon
  • 185
  • 1
  • 3
  • 12
  • How do you try to reach your `like` action? Did you create some link/button for this? Show us the template's code for it. – jdoe May 22 '12 at 15:44

2 Answers2

1

Your route expects a PUT request, while you're issuing a GET request.

You'll need to either access your route via a button_to with :method => :put so that your app is issuing PUT requests (the correct solution), or change your route to use GET requests (the wrong way to make requests which modify state):

      get :like, :on => :member
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Don't do it! It breaka all the thing that are stated in every decent book about Rails: DON'T USE `GET` FOR CHANGING STATE! **I'm curious who upvoted this insane assumption!** – jdoe May 22 '12 at 16:02
  • Unfortunately this is not working. If I change the route to get instead of put no transaction is happening in the db. In the other case, if in my view I add the :method => :put I get an error "wrong number of arguments (0 for 1)". Thank you for your help! – Daniel Garzon May 22 '12 at 16:04
  • As @jdoe said, you shouldn't use the change-the-route solution. The correct way to fix this is to make your app issue a PUT request. – user229044 May 22 '12 at 16:25
1

Replace:

= link_to 'Like', like_category_post_path(@post)

with:

= link_to 'Like', like_category_post_path(@category, @post), method: :put

Or, as I like it:

= link_to 'Like', [@category, @post], method: :put

I think your like has to be:

def like
  @post = Post.find(params[:id])
  respond_to do |format|
    format.html do
      if current_user.like @post
        flash[:notice] = "It's ok, you liked it!"
        redirect_to :back
      else
         flash[:error] = "Something went wrong! Please try again."
         redirect_to show_post_path(@category, @post)
      end
    end
  end
end
jdoe
  • 15,665
  • 2
  • 46
  • 48
  • Hi jdoe, thank you for your help! I am getting closer now. For some reason when I do this the transaction is performed in the db, but the redirect is not working as I stay on http://localhost:3000/categories/1/posts/1/like after clicking in my link. This site is blank so I am not getting any errors, do you know why this can be? When I run rake routes I get this: show_post /categories/:category_id/posts/:id(.:format) posts#show like_category_post PUT /categories/:category_id/posts/:id/like(.:format) posts#like – Daniel Garzon May 22 '12 at 16:14
  • Did you post complete code of your `like` action? It doesn't seem to be incomplete, it IS incomplete! – jdoe May 22 '12 at 16:19
  • Yes my like action is my complete code. I found it somewhere online and it was shared by the gem developer as an example, and I though it was complete. This is my first complex rails application so I am struggling a bit. What do you think is missing? – Daniel Garzon May 22 '12 at 16:23
  • Check my answer after words "Or, as I like it". – jdoe May 22 '12 at 16:29
  • oops! You've already "liked" my answer even w/o waiting for my response... :-) Did you fix your action? – jdoe May 22 '12 at 16:31
  • Thank you so much for your help! Everything seems to be working now. I appreciate everything you did. Have a great day and hopefully I will be able to return the favor one day! – Daniel Garzon May 22 '12 at 16:33