作为可投票的投票?注册?

时间:2016-03-16 22:48:49

标签: ruby-on-rails acts-as-votable

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

根据它的git:https://github.com/ryanto/acts_as_votable它说有一种名为vote_registered的方法?如果用户之前已经投票,则检查投票是否有效。

它说:

要检查投票是否已计算或已注册,请使用vote_registered?投票后在你的模型上。例如:

@ hat.liked_by @user

@ hat.vote_registered? #=>真

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

感谢。

我在我的articles_controller中尝试了这个但是从Action控制器获取错误,例如Missing template articles / 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

1 个答案:

答案 0 :(得分: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