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 7547685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:30:57+00:00 2026-05-30T09:30:57+00:00

I am currently getting an undefined method for adding <% if current_user.vote_up?(@micropost) == true

  • 0

I am currently getting an undefined method for adding <% if current_user.vote_up?(@micropost) == true %>, I am unsure what is causing the error because I defined it here in the micropost controller. Any suggestions?

Micropost Controller

 def vote_up
    @micropost = Micropost.find(params[:id])
    current_user.vote_exclusively_for(@micropost)
    current_user.vote_up(@micropost)
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
      end
  end

   def unvote
    @micropost = Micropost.find(params[:id])
    current_user.vote_exclusively_against(@micropost)
    current_user.unvote(@thred)
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
      end
  end

Micropost HTML

<% if current_user.vote_up?(@micropost) == true %>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/vote_up" data-remote='true' class='CounterButton b2'>
<span id="CounterIcon" class="<%=micropost.id%>"></span>
</a>
</div>
<% else %>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/unvote" data-remote='true' class='CounterButton b2'>
<span id="CounterIcon" class="<%=micropost.id%>"></span>
</a>
</div>
<% end %>
  • 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-05-30T09:30:58+00:00Added an answer on May 30, 2026 at 9:30 am

    Controller methods can’t be called directly like you intend to from the view.

    Furthermore, there seems to be a bit of confusion on how the MVC separation works in Rails.

    Your current_user variable (in the view) corresponds to the “Model” layer, and as such, vote_up? should be a method of whichever class current_user is an instance of. This would be defined in models/User.rb or something like that, and there should be a vote_up? method that just queries the user’s instance and returns a boolean value.

    This has nothing to do with the controller’s vote_up action. This will not return a value per se: I understand it to actually vote up a “micropost”. A method in the controller will be fired in response to a user’s request, do some stuff to objects from the database (model layer), possibly put some of these model objects in variables, and invoke a view to show information to the user (possibly taken from these objects). But methods in the controllers aren’t meant to be “called” from the view.

    Incidentally, doing == true as you do in the Micropost HTML is redundant as by convention, methods ending in ? return a boolean value. You can just:

    <% if current_user.vote_up?(@micropost) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently getting an undefined method and I am unsure how to fix this issue,
I am getting undefined method `member' error in my following code Pls tell where
I'm getting the error: undefined method `model_name' for NilClass:Class when trying to call the
I don't know when it happened but I'm getting this error NoMethodError (undefined method
I'm currently getting a Could not load type UI.Administration.Site.master error message and I'm not
I'm using prepared statements and MySQLi, and am currently getting this error. PHP Catchable
I am getting NoMethodError in SitesController#index undefined method `subdomain' for nil:NilClass I have an
In application_controller I have the following method: def authorized_for_roles(*roles) roles.each{|role_name| return true if current_user.has_role?(role_name)}
I'm getting an undefined reference to one private methods in a class. Here is
i am getting this undefined method for albums controller and model any solutions?... undefined

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.