我使用脚手架创建了一个用于文章的CRUD系统。在控制器中,我看到了这个:
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
什么是
respond_to
,它从哪里来?由于它使用了do
运算符,我假设它是某种可迭代列表。对于该列表中的每个format
,它将执行html
和json
方法。{ render json: @posts }
与json
方法有何关系?render json: @posts
作为参数传递给方法吗?render
和json
是否都是对象?我从未见过在符号之外使用冒号表示法。