Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:07:33+00:00 2026-06-15T07:07:33+00:00

The following code does the following: If the user, voted up show him a

  • 0

The following code does the following: If the user, voted up show him a delete vote form and a vote down form. If the user voted down, show him a vote up form and the delete vote form. Otherwise just show the vote up and vote down form (I omitted some of the code so the question doesn’t get too long).

enter image description here

(scenario: user voted down):

posts_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

posts/vote_form

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

votes/create.js:

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

shared/_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 %>

So, now everything works fine, except that I get this error when I click vote down, and then vote up right after (without refreshing the page):

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

What could be the problem?

EDIT:

I realized that the line in the error message is the problem (not @votable):

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

Strange, I thought it was the same as

<% if @post.votes.find_by_user_id(current_user.id).polarity == 1 %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:07:35+00:00Added an answer on June 15, 2026 at 7:07 am

    I think the problem is here:

    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
    

    You destroy the vote for current user, so it can not be found inside view:

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

    That’s why polarity is called on nil. You should reword the logic of your vote manipulations.

    EDIT:

    As the easiest solution, you may replace

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

    with

    if @votable.votes.find_by_user_id(current_user.id).blank?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which does nothing but reading some values from a
What does the => operator mean in the following code? foreach ($user_list as $user
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
The following code does not time out in Ruby 1.9.3p194 (2012-04-20) [i386-mingw32]: require 'timeout'
The following code does not want to compile. See the included error message. Code:
The following code does not run as rootNode is null when retrieved by name
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not give a warning with g++ 4.1.1 and -Wall .
The following code does not submit the input values: $('.button').live('click', function() { var date
The following code does not compile: //int a = ... int? b = (int?)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.