I am launching some type of problem with an area that prevents the instance variables from initializing properly with 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 a sample / test is displayed, it dies with the error "when evaluating nil.reverse". This is surprising because the first two calls to display were to initialize @display, which I would think of. If the value <% = @ display.reverse%> is deleted, the output will be "aa", which indicates that the @display instance variable gets the installed helper method but does not have access to it in the view.
If the controller is modified to become (with view source code):
class SampleController < ApplicationController def test display end end
The output will be "aa aaa aa". If I make this 2 calls to display in the controller, I get: "aaa aaaa aa aa". Thus, it seems that only calls made in the controller will change the instance of the SampleController instance, while calls in the view will change the instance variable for the ApplicationController, which the view does not have access to.
Is this a bug in Rails, or I donβt understand, and does this mean functionality for some reason?
Is the context in which I encountered this error trying to create logged_in? The ApplicationController method, which sets the @user variable on the first call and returns true or false if the user is logged on. This will not work unless I add an extra call to the controller before trying to use it in the view.
source share