0

I am using the Thumb_Up gem for ruby on rails. https://github.com/brady8/thumbs_up

I want users to be able to vote on posts. However, I am unable to figure out how I can allow a user to click a button next to each post and add a vote to the database.

I can get this to happen in the rails console through doing the following:

u=User.first

m=Micropost.first

u.vote_for(m)

However, how can I get this to happen when a button is clicked in view. I am assuming I would have to use ajax, but how would I know the url I need to post to to make this action occur?

Any help would be greatly appreciated.

Update:

Thanks so much for the help! I am still having a problem with the code below.

Here is my routes.rb

resources :microposts do
  post :vote, :on => :member
end

View:

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

Controller:

def vote
    @micropost = Micropost.find(params[:id])
  current_user.vote_for @micropost

  # This assumes you'll only call it via AJAX.
  # If your ajax call doesn't return "ok", then you know something went wrong
  render :text => 'ok', :layout => false
end

However, I'm still getting this error: No route matches {:controller=>"microposts", :id=>#, :action=>"vote"}

Would anyone know why the routes aren't matching correctly?

chris
  • 39
  • 4
  • Yes that's a little clearer. It looks like in your view, @micropost is not actually being set to a Micropost record. Whatever action is rendering that view needs to set @micropost, just like the _vote_ action is doing. – bioneuralnet Apr 14 '11 at 15:33

1 Answers1

1

I am assuming Rails 3. Rails 2's routes would look a little different.

First you would need to define a route in your config/routes.rb file. You could do this many ways. If you already have a route for microposts, you could simply add a "vote" action:

    resources :microposts do
      post :vote, :on => :member
    end

(For clarity, the "post" above refers to the HTTP POST method and has nothing to do with your Micropost class.) If you use that route, you would then need to create a "vote" method in your Microposts controller to catch it. Something like

    def vote
      @post = Micropost.find(params[:id])
      current_user.vote_for @post

      # This assumes you'll only call it via AJAX.
      # If your ajax call doesn't return "ok", then you know something went wrong
      render :text => 'ok', :layout => false
    end

Then in your view's AJAX POST call (assuming the example route I gave), you would get the url with:

    vote_micropost_path(@micropost)

It would look like /microposts/56/vote

bioneuralnet
  • 5,283
  • 1
  • 24
  • 30
  • I am using this link_to in my view: **bold**<%= link_to('vote for this post!', vote_micropost_path(@micropost), :method => :post) %> **bold**. I keep getting this error No route matches {:controller=>"microposts", :id=>#, :action=>"vote"} Here is what I used for my route as well: resources :microposts, :only => [:create, :destroy] do post :vote, :on => :member end Thank you very much for the help. – chris Apr 12 '11 at 15:22
  • Your route looks fine. I am assuming @micropost is set to a Micropost object you want to vote on? Because it looks like it's a brand new Micropost object that isn't actually in the database. – bioneuralnet Apr 12 '11 at 15:38
  • I used this in the controller to get the current micropost. @micropost = Micropost.find(params[:id]) Would I need to somehow pass in the id of the one being clicked on? How could you go about doing something like that or is there an easier trick. – chris Apr 12 '11 at 15:46
  • I'm not sure I follow. Where is that code in your controller - in the "vote" action or in the action that displays your micropost(s) vote button(s)? And what would the url look like? – bioneuralnet Apr 12 '11 at 17:22
  • I am trying to copy the way you did it. I updated my post above with the code I used and the locations. I put @micropost = Micropost.find(params[:id]) in the "vote" action. I put this in the view: <%= link_to('vote for this post!', vote_micropost_path(@micropost)) %>. Sorry for being unclear. My updated post should make this easier. Thanks so much again! – chris Apr 12 '11 at 19:46
  • Here is a more clear question: How would I set @micropost to the object I want voted on? – chris Apr 13 '11 at 00:04