Thumbs up gem routing error

178 views Asked by At

I seek clarification on using the thumbs up gem with rails 4. I current have a user resource and a post resource and have set up thumbs up as follows.

add gem to gemfile and installed it using bundler. Generated require migration

User model

class User < ActiveRecord::Base
 acts_as_voter
end

Post model

class Post < ActiveRecord::Base
 acts_as_voteable
end

Post controller

def vote_up
 @post = Post.find(params[:id])
 current_user.vote_for(@post)
 respond_to do |format|
   format.js
 end
end

View

<%= link_to('vote for this post!', vote_up_post_path(@post) , :method => :post) %>

Route file

resources :posts do
  member do
   post :vote_up
  end
 end

However i keep getting this error

No route matches [POST] "/posts/vote_up"

And after running rake routes I can see that the following route is available to me:

vote_up_post POST   /posts/:id/vote_up(.:format) posts#vote_up

any ideas what could be the cause of this error ?

4

There are 4 answers

0
NicoArbogast On

Could you please show us your view.

Apparently, you are calling

/posts/vote_up

instead of

/posts/:id/vote_up
2
Billy Chan On

"vote_up" acts like a RESTful action, similar to "post/1/delete", "post/1/edit". So you need to add this custom RESTful action in route.

Change your route like this at first.

resources :posts do
  member do
    post 'vote_up'
  end
end

Then, in your view, to use this path, add resource as arg

vote_up_post_path @post

Reference: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

0
Alex Chin On

Other people reading this notes should realise that post is not a singluar of his controller posts... but the post http verb

resources :contenders do
  member do
   post 'vote_up'
  end
end
0
Trupti Jangam On

Not sure but in your view, instead of @post try giving @post.id to vote_up_post_path.

<%= link_to('vote for this post!', vote_up_post_path(@post.id) , :method => :post) %>