2

I installed the acts_as_votable gem, it works in the console like it should (like it says in the documentation). So my question is how to set up a form for upvote and downvote buttons? or can they simply be links?

here is the documentation: github.com/ryanto/acts_as_votable/blob/master/README.markdown

I have a user and a picture model; the user is supposed to be able to like the picture. code from the picture view, where the buttons should be:

<% for picture in @pictures %> 
<p> 
<%= image_tag picture.image_url(:thumb).to_s %> 
</p> 
<%= picture.created_at.strftime("%a, %d %b. %Y") %>, by 
<%= link_to picture.user.name, picture.user %> 
<h2> <%= link_to picture.name, picture %></h2> 

[buttons here] 

<%= picture.votes.size %> <% end %>
2
  • honestly, I have no idea how to set a form like this up. here is the documentation: github.com/ryanto/acts_as_votable/blob/master/README.markdown I have a user and a picture model; the user is supposed to be able to like the picture. code from the picture view, where the buttons should be: <% for picture in @pictures %> <p> <%= image_tag picture.image_url(:thumb).to_s %> </p> <%= picture.created_at.strftime("%a, %d %b. %Y") %>, by <%= link_to picture.user.name, picture.user %> <h2> <%= link_to picture.name, picture %></h2> [buttons here] <%= picture.votes.size %> <% end %>
    – figdig
    Commented Feb 21, 2013 at 21:34
  • Ok. Add that to your original question Commented Feb 21, 2013 at 21:41

2 Answers 2

9

One way to do this is to add your own controller actions for up- and downvotes. I'm assuming you have a current_user method available in your controller.

# pictures_controller.rb
def upvote
  @picture = Picture.find(params[:id])
  @picture.liked_by current_user
  redirect_to @picture
end

def downvote
  @picture = Picture.find(params[:id])
  @picture.downvote_from current_user
  redirect_to @picture
end

# config/routes.rb

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

# some view

<%= link_to "Upvote", like_picture_path(@picture), method: :put %>
<%= link_to "Downvote", dislike_picture_path(@picture), method: :put %>
1
  • 2
    There's a typo in the view code; method: :put needs to be passed to link_to, not the path helper. Cheers :). Commented Jan 18, 2014 at 17:11
5

Here's how I ended up doing it with the acts_as_commentable gem too. So I think this should work with any object that you have comments for.

In my _comment.html.erb view

<%= link_to "Upvote", {:controller =>"comments", :action => "upvote", :id => comment.id}, :class => 'btn', method: :put %>
<%= link_to "Downvote", {:controller =>"comments", :action => "downvote", :id => comment.id}, :class => 'btn', method: :put %>

in my routes.rb file

put '/comments/:id/:action' => 'comments#upvote'
put '/comments/:id/:action' => 'comments#downvote'

Then in my comments controller

class CommentsController < ApplicationController
  before_filter :load_commentable
  before_filter :find_comment, :only => [:upvote, :downvote]



  def upvote
    current_user.upvotes @comment
    redirect_to(@comment.commentable)
  end

  def downvote
    @comment.downvote_from current_user
    redirect_to(@comment.commentable)
  end




private

  def load_commentable
    resource, id = request.path.split('/')[1, 2]
    @commentable = resource.singularize.classify.constantize.find(id)
  end

  def find_comment
    @comment = Comment.find(@commentable.id)
  end


end

The before filters allow for more versatility so I can add this to any commentable object. Mine happened to be festivals, but you could do pictures or anything really. Check out the acts_as_commentable documentation and the polymorphic railscast for more information on that. This is my first post so if this is terrible code, just tell me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.