Ruby on Rails:初始化具有帮助器的实例变量 [英] Ruby on Rails: Initializing instance variables with helpers in view

查看:85
本文介绍了Ruby on Rails:初始化具有帮助器的实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了某种范围问题,该问题阻止实例变量被视图调用的助手正确初始化.

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

当显示样品/测试时,它死于评估nil.reverse"时出错.这是令人惊讶的,因为前两个对display的调用应该已经初始化了@display.如果删除了<%= @ display.reverse%>,则输出为"aa",表示@display实例变量已通过helper方法进行设置,但视图中无权对其进行访问.

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

输出变为"aa aaa a a".如果我在控制器中进行2次调用以显示,我将得到"aaa aaaa aa aa".因此,似乎只有在控制器中进行的调用才能修改SampleController实例变量,而在视图中的调用将为该视图无法访问的ApplicationController修改实例变量.

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.

这是Rails中的错误还是我误解了,并且出于某种原因,这是预期的功能?

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

我遇到此错误的上下文正在尝试创建一个logging_in?首次设置@user变量的ApplicationController方法,如果用户已登录,则返回true或false.除非尝试在视图中使用它之前,我在控制器中添加了不必要的调用,否则该方法无效.

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.

推荐答案

在渲染时有两个@display ivars,一个在控制器中,一个在视图中.该视图无法访问控制器中的视图,反之亦然.在渲染开始时,Rails将所有控制器ivars复制到视图中.但是那时@display不存在并且不会被复制.在任何红宝石程序中,对@my_undefined_ivar的调用都将返回nil-这正是您正在发生的事情.

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.

可能会造成混淆,这是另一种说法. Rails在渲染开始时将ivars从控制器复制到视图中.因此,视图的ivar中的更改不会反映在控制器ivar中,反之亦然.您定义的帮助程序允许视图调用控制器上的方法,以便可以将控制器的ivar作为返回值传递回视图,但对控制器ivar本身的更改不会反映在另一个视图的ivar中.

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.

在这种情况下,只需使用helper方法,而无需在视图中使用@display.

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

这篇关于Ruby on Rails:初始化具有帮助器的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆