这个块的解释是什么?
我已经使用脚手架创建了一些模块,但我不明白我们如何在不与对象或任何东西相关的情况下使用respond_to?
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :ok }
end
我研究了“do”,发现它就像“each”处理可枚举对象
I have created some modules using scaffold, but I can't understand how can we use respond_to without relating to an object or anything?
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :ok }
end
I have researched about the 'do' and found that it is like 'each' deals with enumerable object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
do
和end
之间的部分是一个 ruby 块。如果您熟悉匿名函数,它与这个概念有一些相似之处。respond_to
是控制器上的一个实例方法;该块被传递给此方法。|format|
是此方法在执行的某个时刻传递给块的参数。块的好处是它们在方法调用者的上下文(“绑定”)中进行评估,在本例中是控制器的实例。因此,您可以在块内的控制器中使用任何可以使用的变量或方法 - 即使您正在调用另一个对象上的方法。这是 Ruby 的一个非常强大的功能,正如 @Niklaos 和 @Perry 所说,您绝对应该了解更多有关 Ruby 习惯用法的知识,因为这些块无处不在!
块的另一种语法是
还有一件事:许多迭代器方法在 ruby 中使用块,但块并不限于迭代。例如,“each”是一个迭代器,它逐个生成(传递到块)集合中调用它的每个成员。
因此,为了回答您的问题,
respond_to
方法产生一个ActionController::MimeResponds::Collector
对象到块;在此块中,您可以为不同的 MIME 类型配置不同的响应。为此,您可以将另一个块传递给该对象的一个或多个 MIME 方法(html
、json
等)。然后,控制器使用此 Collector 对象来确定对请求的适当响应(呈现 html.erb 模板,或将内容格式化为 json 等 - 请注意,例如,redirect_to 确实是控制器实例方法)。 可以在此处找到很好的 respond_to 文档。
希望这有帮助!
the part between
do
andend
is a ruby block. If you're familiar with anonymous functions, it shares some similitudes with this concept.respond_to
is an instance method on your controller ; the block is passed to this method.|format|
is the argument that this method passes to the block at some point of its execution.the good thing with blocks is that they are evaluated in the context ("binding") of the method caller, in this case the instance of your controller. So you can use any variables or methods you could use in your controller inside your block - even if you were calling a method on another object. This is a very powerful feature from Ruby, and as @Niklaos and @Perry said, you should definitely learn more about ruby idioms because these blocks are everywhere !
the other syntax for a block is
One more thing : many iterator methods use blocks in ruby, but blocks are not limited to iteration. 'each' for example is an iterator that yields (passes to the block) every member of a collection from which it is called on, one after another.
So, to answer your question the
respond_to
method yields anActionController::MimeResponds::Collector
object to the block ; in this block you configure different responses for different MIME types. To do so you pass another block to one or many of the MIME methods (html
,json
,etc.) of this object.The controller then uses this Collector object to determine the appropriate response to a request (render an html.erb template, or format content as json, etc. - note that redirect_to, for example, is indeed a controller instance method). A good documentation for respond_to can be found here.
Hope this helped !
我认为这个概念称为上下文探测。
这意味着该函数实际上并未在其编写的上下文中执行。
因此,您的函数实际上是在 respond_to (以及其他方法)可用的上下文中执行的。
这本书将解释一切,如果你喜欢 ruby,这本书会是非常棒的读物。
I think this concept is called context probing.
Meaning the function is not actually being executed in the context where it's written.
So your function is in fact executed in a context where respond_to (among other methods) is available.
This book will explain everything, it's pretty amazing reading if you like ruby.