caches_action if子句中的proc如何执行
我有一个新手问题,我无法理解。 caches_action
方法的 if 条件中的 Proc 如何执行。
例如:
caches_action :show, :if=>Proc.new{|x| something}
我不明白的是如何调用它。 过程
我知道我可以执行定义为proc= Proc.new
的 proc.call
所以我不明白这是如何调用的。
其次,如何传递诸如
if Logged_in?
这样的条件,我将不胜感激。
I have a newbie kind of question which I can't get my head around. How is the Proc in the if condition get executed for the caches_action
method.
For example:
caches_action :show, :if=>Proc.new{|x| something}
What I don't get is how this gets called.
I know I can execute a proc defined as
proc= Proc.new
byproc.call
so I don't understand how this gets called.
Second, how do I pass conditions like
if logged_in?
I'd appreciate any help on this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Proc上传递的参数是当前对象。因此,在您的示例中,它是
x
变量。这样就可以调用该实例的所有方法。如果您想调用logged_in?
方法。你可以,因为它是一个公共实例,该过程在过滤器之前被调用。一个caches_action就像一个before_filter。该过滤器检查是否已经有关于该操作的缓存。除非,缓存已生成。
使用
:if
仅当调用 if 时才调用过滤器。所以过程是调用的。如果您不使用 Proc,则仅在服务器启动时读取文件期间解释:if
值。The parameter pass on Proc is the current object. So in your example it's the
x
variable. So you can call all method of this instance. If you want call thelogged_in?
method. You can because it's a public instanceThe proc is call before the filter. A caches_action is like a before_filter. This filter check if there are already a cache about this action or not. Unless, the cache is generate.
With the
:if
the filter is call only if the if is call. So the proc is call. If you don't use a Proc, the:if
value is interpret only during the file reading on the server starting.<代码>:如果=>过程 { 登录? 我的理解
是你要向它发送一个块来评估。因此,如果您在帮助程序或登录控制器中有一个方法,它会调用该方法并进行评估。我相信是控制器类调用了 proc。
来自文档; “如果给出了一个块,则会使用当前控制器实例来调用它。”
http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html
:if => proc { logged_in? }
My understanding is that you're sending it a block to evaluate. So you if you have a method in a helper or in the controller logged_in, it will call that and evaluate. I believe it's the controller class that is calling the proc.
From documentation; "If a block is given, it is called with the current controller instance."
http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html