0

我已经在我的 Rails 应用程序中实现了可投票的行为,并且一切顺利。

根据它的 git:https ://github.com/ryanto/acts_as_votable它说有一种方法叫做 vote_registered?如果用户之前已经对其进行过投票,则检查该投票是否有效。

它说:

要检查投票是否计入或登记,请使用 vote_registered? 投票后在您的模型上。例如:

@hat.liked_by @user

@hat.vote_registered?# => 真

我只是想要一些帮助来尝试实现它。

谢谢。

我在我的articles_controller 中尝试过这个,但是从动作控制器中得到错误,比如缺少模板文章/upvote,application/upvote with {:locale=>[:en], :formats=>[:html], :variants=>[] , :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :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
4

1 回答 1

0

好的,我想通了。我很接近,只需要将用户推回现有页面。

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"
      respond_to do |format|
        format.html {redirect_to :back }
      end
    end
  end
  def downvote
    @article.downvote_by current_user
    if @article.vote_registered?
      flash[:success] = "Successfully disliked"
      respond_to do |format|
        format.html {redirect_to :back }
        format.json { render json: { count: @article.get_downvotes.size } }
     end
    else
      flash[:danger] = "You have already disliked this"
      respond_to do |format|
        format.html {redirect_to :back }
      end
    end
  end
于 2016-03-16T22:56:57.877 回答