让我们说我有一个这样的帮手:
module ApplicationHelper
def foo
@foo
end
def set_foo(foo)
@foo = foo
end
def foo=(foo)
@foo = foo
end
end
和这样的控制器:
class ApplicationController < ActionController::Base
include ApplicationHelper
def index
foo = 'hello'
end
end
我想知道为什么在控制器中foo = 'hello'
和foo=('hello')
都不调用助手的方法foo=
,但set_foo 'hello'
会调用其方法{{1} }}?
答案 0 :(得分:4)
简短的回答是,它只会在名为foo
的控制器方法中设置一个局部变量。
如果你打电话给self.foo = 'hello'
,Ruby会知道foo是一个应该调用的方法。
值得注意的是,这是对Rails视图助手的完全错误使用。