6
votes

I am new to Rails so please have patience.

I wanted to implement "like" on my canteen model, so I created a custom route inside my canteen resource

resources :canteens do
  resources :meals
  resources :comments
  match "/like", :to => "canteens#like", :as => "like"
end

and therefore created this action inside canteens controller, where I just increment a counter

def like    
  canteen = Canteen.find(params[:canteen_id])
  Canteen.increment_counter("likes_count", canteen.id)
  redirect_to canteen
end

So, manually entering the URL localhost:3000/canteens/1/like works just fine, however obviously I want to create a button so I did a

<h2>Likes count</h2>
<p><%= @canteen.likes_count %> likes</p>
<p><%= link_to "Like this canteen", canteen_like_path %></p>

And it doesn't work. I checked rake routes and it's there (canteen_like). What am I doing wrong?

1
Can you show your rake routes output?Leo Correa

1 Answers

10
votes

You have to pass a Canteen object to the path. If you don't do that Rails doesn't know which canteen you meant:

<%= link_to "Like this canteen", canteen_like_path(@canteen) %>