1

以下代码执行以下操作:如果用户,投票给他一个删除投票表格和一个投票反对表格。如果用户投了反对票,则向他展示投赞成票删除投票表。否则,只需显示赞成票和反对票表格(我省略了一些代码,因此问题不会太长)。

在此处输入图像描述

(场景:用户投反对票):

post_controller.rb:

  def show
    @post = Post.find(params[:id])
    @replies = @post.replies.paginate(page: params[:page])
    @reply = @post.replies.build
    @vote = Vote.new
    store_location
  end

votes_controller.rb:

class VotesController < ApplicationController
 before_filter :signed_in_user

 def create
    @votable = find_votable

    # Destroy the vote first in case the user already voted
    if already_voted?
      @vote = @votable.votes.find_by_user_id(current_user.id)
      @vote.destroy
    end

    @vote = @votable.votes.build(params[:vote])
    @vote.user_id = current_user.id
    @votable.save

    respond_to do |format|
      format.html { redirect_back }
      format.js
    end
  end

  def destroy
    @votable = find_votable

    @vote = @votable.votes.find_by_user_id(current_user.id)
    @vote.destroy
    @votable.reload

    respond_to do |format|
      format.html { redirect_back }
      format.js
    end
  end


  private

    def find_votable
      params.each do |name, value|
        if name =~ /(.+)_id$/
          return $1.classify.constantize.find(value)
        end
      end
      nil
    end

    def already_voted?
      @votable.votes.exists?(:user_id => current_user.id)
    end
end

帖子/投票表格

<div class="vote-form">
  <% if @post.votes.exists?(:user_id => current_user.id) %>
    <% if @post.votes.find_by_user_id(current_user.id).polarity == -1 %>
      <%= form_for ([@post, @vote]), remote: true do |f| %>
        <%= f.hidden_field :polarity, value: 1 %>
        <div class="form-actions">
          <%= button_tag type: :submit, class: "btn btn-small vote" do %>
            <i class="icon-thumbs-down"></i> Vote up
          <% end %>
        </div>
      <% end %>

      <%= form_for ([@post, @post.votes.find_by_user_id(current_user.id)]),
                                                           method: :delete,
                                                           remote: true do |f| %>
        <div class="form-actions">
          <%= button_tag type: :submit, class: "btn btn-small btn-primary unvote" do %>
            <i class="icon-thumbs-up"></i> Vote down
          <% end %>
        </div>
      <% end %>
    <% end %>

投票/create.js:

$('#<%= @votable.class.name.downcase %>-<%= @votable.id %> .vote-form').html(
  "<%= escape_javascript(render('shared/delete_vote')) %>"
);

共享/_delete_vote.html.erb:

<% if @votable.votes.find_by_user_id(current_user.id).polarity == -1 %>
  <%= form_for ([@votable, @votable.votes.new]), remote: true do |f| %>
    <div class="form-actions">
      <%= button_tag type: :submit, class: "vote-down btn btn-small" do %>
        <i class="icon-thumbs-up"></i> Vote up
      <% end %>
    </div>
  <% end %>

  <%= form_for ([@votable, @vote]), method: :delete, remote: true do |f| %>
    <div class="form-actions">
      <%= button_tag type: :submit, class: "vote-up btn btn-small btn-primary" do %>
        <i class="icon-thumbs-up"></i> Vote down
      <% end %>
    </div>
  <% end %>
<% end %>

所以,现在一切正常,除了当我单击vote down时出现此错误,然后立即投票(不刷新页面):

ActionView::Template::Error (undefined method `polarity' for nil:NilClass):
    1: <% if @votable.votes.find_by_user_id(current_user.id).polarity == 1 %>
    2:   <%= form_for ([@votable, @vote]), method: :delete, remote: true do |f| %>

可能是什么问题呢?

编辑:

我意识到错误消息中的行是问题(不是@votable):

<% if @votable.votes.find_by_user_id(current_user.id).polarity == 1 %>

奇怪,我还以为是一样的

<% if @post.votes.find_by_user_id(current_user.id).polarity == 1 %>
4

1 回答 1

1

我认为问题出在这里:

def destroy
  @votable = find_votable

  @vote = @votable.votes.find_by_user_id(current_user.id)
  @vote.destroy
  @votable.reload

  respond_to do |format|
    format.html { redirect_back }
    format.js
  end
end

您破坏了当前用户的投票,因此在视图中找不到它:

@votable.votes.find_by_user_id(current_user.id).polarity

这就是为什么极性被称为 nil 的原因。你应该改写你的投票操作的逻辑。

编辑:

作为最简单的解决方案,您可以更换

if @votable.votes.find_by_user_id(current_user.id).polarity == -1

if @votable.votes.find_by_user_id(current_user.id).blank?
于 2012-12-02T06:18:22.667 回答