Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

ruby on rails - acts_as_votable thumbs up/down buttons

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 %>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...