Ruby on Rails 中的@变量 [英] @ variables in Ruby on Rails
问题描述
@title
和 title
有什么区别?因为它们都可以是变量名.另外,我如何决定应该使用哪种变量?有没有@
?
What's the difference between @title
and title
? Since both of them can be variable names. Also, how do I decide which kind of variable I should use? With @
or not?
推荐答案
title
是一个局部变量.它们只存在于其范围内(当前块)
title
is a local variable. They only exists within its scope (current block)
@title
是一个实例变量 - 可用于类中的所有方法.
@title
is an instance variable - and is available to all methods within the class.
您可以在此处阅读更多信息:http://strugglingwithruby.blogspot.dk/2010/03/variables.html
You can read more here: http://strugglingwithruby.blogspot.dk/2010/03/variables.html
在 Ruby on Rails 中 - 将控制器中的变量声明为实例变量 (@title
) 使它们可用于您的视图.
In Ruby on Rails - declaring your variables in your controller as instance variables (@title
) makes them available to your view.
这篇关于Ruby on Rails 中的@变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!