扩展控制器中的操作
我正在构建一个在多个操作中设置相同变量的控制器。像这样的事情:
def one
@a = 1
@b=2
@test = "One"
end
def two
@a = 1
@b = 2
@test = "Two"
end
我知道我可以调用一种方法来填充变量赋值,但我想知道如何以“最佳实践”方式做到这一点。我雄心勃勃并尝试过……
def master
@a = 1
@b = 2
end
def one < master
@test = "One"
end
def two < master
@test = "Two"
end
但这并没有成功。 SO 社区有什么建议?
I'm building a controller that sets the same variables in several actions. Something like this:
def one
@a = 1
@b=2
@test = "One"
end
def two
@a = 1
@b = 2
@test = "Two"
end
I'm aware that I could call a method to fill in the variable assignments, but I'm wondering how one would do this the "Best Practice" way. I got ambitious and tried...
def master
@a = 1
@b = 2
end
def one < master
@test = "One"
end
def two < master
@test = "Two"
end
But this arose to no avail. What does the SO community suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
<
用于 Ruby 中的继承,不能用于方法。在 Rails 中,您可以调用before_filter
来实现此目的。如果您希望将其用于控制器中的所有方法,或者
仅希望将其用于这些方法。
<
is used for inheritance in Ruby and cannot be used on methods. In Rails you can callbefore_filter
for this purpose.if you want it for all methods in the controller, or
if you want it for these methods only.