0
我已经在我的Rails应用中实现了可投票的行为,并且一切顺利。作为可投票vote_registered?
根据它的git:https://github.com/ryanto/acts_as_votable它说有一种方法叫vote_registered?检查投票是否有效,如果用户以前已投票。
它说:
要检查选票计数,或注册,使用vote_registered?投票后在您的模型上。例如:
@ hat.liked_by @user
@ hat.vote_registered? #=> true
我只是想试一试它的一些帮助。
谢谢。
我已经在我的articles_controller中尝试了这一点,但从操作控制器中获取错误,如丢失模板文章/ upvote,application/upvote with {:locale => [:en],:formats => [:html],:variants => []:处理程序=> [:ERB,:建设者,:生,:红宝石,:咖啡:JBuilder中]}
def upvote
@article.upvote_by current_user
if @article.vote_registered?
flash[:success] = "Successfully liked"
respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @article.get_upvotes.size } }
end
else
flash[:danger] = "You have already liked this"
end
end
从与当前工作的投票我的文章控制器:
def upvote
@article.upvote_by current_user
flash[:success] = "Successfully liked"
respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @article.get_upvotes.size } }
end
end
def downvote
@article.downvote_by current_user
flash[:success] = "Successfully disliked"
respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @article.get_downvotes.size } }
end
end
产品型号:
class Article < ActiveRecord::Base
validates :title, presence: true
validates :body, presence: true
belongs_to :user
acts_as_votable
has_many :comments, dependent: :destroy
default_scope { order(created_at: :desc)}
end
从网页投票时:
<h1 class="article-detail-title"><%= @article.title %></h1>
<div class="votes" id="likes-dislikes">
<hr>
<%= link_to like_article_path(@article), method: :put, class: 'voting' do %>
<i class="fa fa-thumbs-up"></i>
<%= @article.get_upvotes.size %>
<% end %>
<%= link_to dislike_article_path(@article), method: :put, class: 'voting' do %>
<i class="fa fa-thumbs-down"></i>
<%= @article.get_downvotes.size %>
<% end %>
<div class="glyphicon glyphicon-calendar" id="article-date">
<%= @article.created_at.strftime("%b %d, %Y") %>
</div>
<hr>
</div>
路线:
Rails.application.routes.draw do
devise_for :users, :controllers => {registrations: "registrations", sessions: "sessions", :omniauth_callbacks => "callbacks"}
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root to: 'articles#index'
resources :articles do
member do
put "like", to: "articles#upvote"
put "dislike", to: "articles#downvote"
end
resources :comments
end
end