Ruby on Rails: initializing instance variables with helpers

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.

+4
source share
2 answers

There are two @display ivars during rendering, one in the controller and one in the view. A view cannot access this in the controller and vice versa. At the beginning of the rendering, Rails copies all the ivars to the view. But at that time @display does not exist and is not copied. In any ruby ​​program, calling @my_undefined_ivar will return nil - this is exactly what happens to you.

Maybe this is confusing, it says otherwise here. Rails copies ivars from the controller to the view at the beginning of the rendering. Thus, changes in ivar representation are not reflected in the ivar controller and vice versa. The helper you defined allows the view to invoke a method on the controller so that the ivar controller can be returned to the view as a return value, but changing the ivar controller itself is not reflected in another ivar view.

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

+6
source

I think this may be the second reserved word that I saw this week ...

Tip. You almost never need to use the return keyword in Ruby. Also, is your usage defined? I was beaten ... maybe it's worth a try:

  if @display.nil? @display = "a" else @display += "a" end 
0
source

Source: https://habr.com/ru/post/1299264/


All Articles