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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:26:27+00:00 2026-05-13T13:26:27+00:00

I’m running into some type of a scope issue that’s preventing instance variables from

  • 0

I’m running into some type of a scope issue that’s preventing instance variables from being properly initialized by helpers called from the view.

#sample_controller.rb
class SampleController < ApplicationController
  def test
  end
end

#application_controller.rb
helper_method :display 
def display
  if not defined? @display
    return @display = "a"
  else
    return @display += "a"
  end
end

#test.html.erb
<%= display %>
<%= display %>
<%= @display %>
<%= @display.reverse %>

When sample/test is displayed, it dies with an error “while evaluating nil.reverse”. This is surprising because the first two calls to display should have initialized @display I would have thought. If <%= @display.reverse %> is removed the output is “a aa”, indicating that the @display instance variable is getting set by the helper method, but there is no access to it in the view.

If the controller is modified so to become (with the original view code):

class SampleController < ApplicationController
  def test
    display
  end
end

The output becomes “aa aaa a a”. If I make it 2 calls to display in the controller, I get, “aaa aaaa aa aa”. So it seems like only the calls made in the controller will modify the SampleController instance variable, whereas calls in the view will modify an instance variable for the ApplicationController that the view has no access to.

Is this a bug in Rails or am I misunderstanding and this is for some reason intended functionality?

The context in which I ran into this bug is trying to create a logged_in? ApplicationController method that sets up an @user variable the first time it is called, and returns true or false if a user is logged in. This would not work unless I added an unnecessary call in the controller before attempting to use it in the view.

  • 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-13T13:26:28+00:00Added an answer on May 13, 2026 at 1:26 pm

    There are two @display ivars at render time, one in the controller and one in the view. The view can’t access the one in the controller and vice versa. At the start of the render Rails copies all the controller ivars into the view. But at that time @display does not exist and is not copied over. In any ruby program a call to @my_undefined_ivar will return nil – which is exactly what is happening to you.

    Could be confusing, here it is said another way. Rails copies ivars from the controller into the view at the start of the render. So, changes in the view’s ivar are not reflected in the controller ivar and vice versa. The helper you defined lets the view invoke a method on the controller so the controller’s ivar can be passed back to the view as a return value but the change to the controller ivar itself is not reflected in the other, the view’s, ivar.

    Just use the helper method in this case and forget about @display in the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 390k
  • Answers 390k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check the docs: http://qbnz.com/highlighter/geshi-doc.html#adding-ids-to-each-line So you have to do $geshi->enable_ids(true);… May 15, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer Check this Chinese menu of jQuery drop downs, you are… May 15, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer You could leverage ArrayObject (provided you're using PHP 5.3) to… May 15, 2026 at 1:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.