[英]Rails helper methods: @ vs normal variable
In Michael Hartl's rails tutorial we have a current_user
method defined as such: 在Michael Hartl的rails教程中,我们定义了
current_user
方法,如下所示:
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
At first I assumed @current_user
was needed as opposed to some other local variable like the_current_user
(assuming you can't use current_user
since that's the name of the method. 起初,我假设需要
@current_user
而不是其他一些局部变量,例如the_current_user
(假设您不能使用current_user
因为这是方法的名称。)
When using helper methods that return something, do we need an @
variable or can we just use any variable type? 使用返回某些内容的辅助方法时,我们需要
@
变量还是仅使用任何变量类型? (I saw other helper_methods use normal variable_names
). (我看到其他helper_method使用普通的
variable_names
)。 (Im assuming @current_user
was just convenient) (我假设
@current_user
只是方便)
@
makes variables available throughout the class. @
使变量在整个类中可用。
If was just current_user
instead of @current_user
it would only be accessible inside of that specific method. 如果只是
current_user
而不是@current_user
则只能在该特定方法内部访问。
You can use current_user
as a variable name inside of a method named current_user
您可以在名为
current_user
的方法内将current_user
用作变量名。
you don't need to use @
before an instance variable to return a value. 您无需在实例变量返回值之前使用
@
。
def some_method
user = "Jimmy"
user
end
> puts some_method
"Jimmy"
Here's a common Ruby idiom, up close: 这是一个常见的Ruby习语,即将结束:
def current_user
@current_user ||= User.find_by(id: something)
end
You can call current_user
as often as you like, now, because it will only spend time hitting the database the first time you call it. 现在,您可以随意调用
current_user
,因为它只会花时间在您第一次调用数据库时访问数据库。 On subsequent calls, @current_user
has a value, so the ||=
evaluates trivially as @current_user = @current_user
. 在随后的调用中,
@current_user
有一个值,因此||=
@current_user = @current_user
为@current_user = @current_user
。
The effect lasts as long as the @
instance exists. 只要
@
实例存在,效果就会持续。 If it's a controller, it will last for the current action, and then disappear. 如果是控制器,它将持续执行当前操作,然后消失。 This means a new action with a different user will not trip over the previous value of
@current_user
. 这意味着使用其他用户执行的新操作不会超出
@current_user
的先前值。
Because def current_user
occupies a namespace different from @current_user
, the Ruby idiom is to name the "memento pattern" variable the same as the method it optimizes. 由于
def current_user
占用的名称空间不同于@current_user
,因此Ruby惯用语是将“ memento pattern”变量命名为与其优化的方法相同的名称。
Your example makes the memento pattern a little confusing, because there are two ways to generate a current user. 您的示例使备忘录模式有些混乱,因为生成当前用户有两种方法。
You can return normal variable in helper methods. 您可以在辅助方法中返回普通变量。
@
is just a syntax used to define an instance variable. @
只是用于定义实例变量的语法。
@current_user
is just a convention, you can use any name like @whatever_user
@current_user
只是一个约定,您可以使用任何名称,例如@whatever_user
For example, if we are using current_user
instead of @current_user
then you will not be able to access current_user
from any of your views. 例如,如果我们使用
current_user
而不是@current_user
则您将无法从任何视图访问current_user
。
For example, if you want to show the name of the user if the user is signed in: 例如,如果要显示用户名(如果用户已登录):
<%= @current_user.name if @current_user %>
It is possible only because we have used the instance variable. 仅因为我们使用了实例变量,才有可能。
Following will help you in knowing more about instance variable: 以下内容将帮助您更多地了解实例变量:
https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/instancevars.html http://ruby-for-beginners.rubymonstas.org/writing_classes/instance_variables.html https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/instancevars.html http://ruby-for-beginners.rubymonstas.org/writing_classes/instance_variables.html
To know about all the types of variables available in Ruby: https://www.studytonight.com/ruby/types-of-variables-in-ruby 要了解Ruby中可用的所有变量类型: https : //www.studytonight.com/ruby/types-of-variables-in-ruby
@current_user ||= User.find_by(id: user_id)
This pattern is called memoization. 这种模式称为记忆。 It is a very common pattern in Ruby/Rails.
这是Ruby / Rails中非常常见的模式。 You can read more about it here:
你可以在这里读更多关于它的内容:
https://www.justinweiss.com/articles/4-simple-memoization-patterns-in-ruby-and-one-gem/ https://www.justinweiss.com/articles/4-simple-memoization-patterns-in-ruby-and-one-gem/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.