Rails:respond_to 块是如何工作的? [英] Rails: How does the respond_to block work?

查看:24
本文介绍了Rails:respond_to 块是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Rails 入门指南,但对 6.7 节感到困惑.生成脚手架后,我在控制器中找到以下自动生成的块:

I'm going through the Getting Started with Rails guide and got confused with section 6.7. After generating a scaffold I find the following auto-generated block in my controller:

def index
  @posts = Post.all

  respond_to do |format|
    format.html  # index.html.erb
    format.json  { render :json => @posts }
  end
end

我想了解 response_to 块的实际工作原理.什么类型的变量是格式?是格式对象的 .html 和 .json 方法吗?

I'd like to understand how the respond_to block actually works. What type of variable is format? Are .html and .json methods of the format object? The documentation for

ActionController::MimeResponds::ClassMethods::respond_to

不回答问题.

推荐答案

我是 Ruby 的新手,却被同样的代码困住了.我挂断的部分比我在这里找到的一些答案更基本一点.这可能会也可能不会帮助某人.

I am new to Ruby and got stuck at this same code. The parts that I got hung up on were a little more fundamental than some of the answers I found here. This may or may not help someone.

  • respond_to 是超类 ActionController 上的一个方法.
  • 它需要一个块,就像一个委托.该块从 doend,其中 |format| 作为块的参数.
  • respond_to 执行您的块,将响应者传递到 format 参数中.
  • respond_to is a method on the superclass ActionController.
  • it takes a block, which is like a delegate. The block is from do until end, with |format| as an argument to the block.
  • respond_to executes your block, passing a Responder into the format argument.

http://api.rubyonrails.org/v4.1/类/ActionController/Responder.html

  • Responder 不包含用于 .html.json 的方法,但我们仍然调用这些方法!这部分让我看不懂.
  • Ruby 有一项名为 method_missing 的功能.如果您调用一个不存在的方法(例如 jsonhtml),Ruby 会改为调用 method_missing 方法.
  • The Responder does NOT contain a method for .html or .json, but we call these methods anyways! This part threw me for a loop.
  • Ruby has a feature called method_missing. If you call a method that doesn't exist (like json or html), Ruby calls the method_missing method instead.

http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html

  • Responder 类使用它的 method_missing 作为一种注册.当我们调用json"时,我们告诉它通过序列化为 json 来响应带有 .json 扩展名的请求.我们需要不带参数调用 html 来告诉它以默认方式(使用约定和视图)处理 .html 请求.
  • The Responder class uses its method_missing as a kind of registration. When we call 'json', we are telling it to respond to requests with the .json extension by serializing to json. We need to call html with no arguments to tell it to handle .html requests in the default way (using conventions and views).

可以这样写(使用类似 JS 的伪代码):

It could be written like this (using JS-like pseudocode):

// get an instance to a responder from the base class
var format = get_responder()

// register html to render in the default way
// (by way of the views and conventions)
format.register('html')

// register json as well. the argument to .json is the second
// argument to method_missing ('json' is the first), which contains
// optional ways to configure the response. In this case, serialize as json.
format.register('json', renderOptions)

这部分让我很困惑.我仍然觉得它不直观.Ruby 似乎相当多地使用这种技术.整个类(responder)成为方法的实现.为了利用method_missing,我们需要一个类的实例,因此我们必须传递一个回调,它们将类方法对象传递到其中.对于使用类 C 语言编码 20 年的人来说,这对我来说是非常落后和不直观的.不是说不好!但这是很多具有这种背景的人需要了解的事情,我认为这可能是 OP 所追求的.

This part confused the heck out of me. I still find it unintuitive. Ruby seems to use this technique quite a bit. The entire class (responder) becomes the method implementation. In order to leverage method_missing, we need an instance of the class, so we're obliged to pass a callback into which they pass the method-like object. For someone who has coded in C-like languages for 20 some years, this is very backwards and unintuitive to me. Not that it's bad! But it's something a lot of people with that kind of background need to get their head around, and I think might be what the OP was after.

附言请注意,在 RoR 4.2 中,respond_to 被提取到 responders gem 中.

p.s. note that in RoR 4.2 respond_to was extracted into responders gem.

这篇关于Rails:respond_to 块是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆