Rails、响应块和 |格式| [英] Rails, respond_to blocks and |format|
问题描述
Rails scaffold
生成以下内容:
Rails scaffold
generated the following:
respond_to do |format|
if @student.save
format.html { redirect_to @student, notice => 'Student was successfully created.' }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
阅读这篇后,我明白了respond_to
正在工作(有点),但我不明白 format
在做什么.不应该是 either format.html
或 format.json
而不是 both 吗?这两行实际上在做什么?
After reading this I understand how the respond_to
is working (sort of), but I don't get what format
is doing. Shouldn't it be either format.html
or format.json
and not both? What are these two lines actually doing?
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
里面是否有隐含的if
?是不是类似
Is there an implied if
in there? Is it something like
if (format == html) {}
if (format == json) {}
旁注:为什么 update
需要 respond_to
块而 show
将处理 /students/1.json
> 还是 /students/1
没有任何逻辑?
Side note: Why does update
require the respond_to
block while show
will handle /students/1.json
or /students/1
without any logic at all?
推荐答案
format
是一个局部变量 respond_to
产量.当您执行 format.html {}
时,您实际上是在为某种格式注册一个回调块.
format
is a local variable that respond_to
yields. When you do format.html {}
you are actually registering a callback block for a format.
Rails 遍历已注册的格式并尝试找到与 请求中的 MIME 类型.如果没有处理程序,它将引发错误.
Rails goes through the registered formats and tries to find a compatible format to the MIME type in the request. If there is no handler it will raise an error.
这可以解释为类似于在 case
语句(相当于 switch 语句的 Ruby 等价物)之上使用语法糖.但是这个类比并不完全准确,因为 Rails 在匹配请求类型方面做了一些工作.
This could be explained as something like using syntactic sugar on top of a case
statement (the Ruby equivalent of a switch statement). But the analogy is not completely accurate since Rails does a bit of work in matching the request type.
当 format.html
块被注册时(就像它只是一个条件语句一样),你的块中的代码也不会执行,而是在 respond_to
时执行> 如果您正在使用例如电子标签缓存,则完成或根本不完成.
Also the code inside your block is not executed when the format.html
block is registered (as it would be if it was just a conditional statement) but rather when respond_to
finishes or not at all if you are using for example E-Tag caching.
为什么更新需要 respond_to 块而 show 会处理/students/1.json 或/students/1 完全没有任何逻辑?
Why does update require the respond_to block while show will handle /students/1.json or /students/1 without any logic at all?
Rails 通过使用约定优于配置方法并猜测操作的意图来处理许多操作.
Rails handles many actions by using a convention over configuration approach and guessing the intent of the action.
def PostsController < ApplicationController
def index
# rails auto-magically fills in the controller with something
# like this
@posts = Post.all
respond_to do |format|
format.html { render :index }
format.json { render json: @posts }
end
end
def show
# convention over configuration is awesome!
@post = Post.find(params[:id])
respond_to do |format|
format.html { render :show }
format.json { render json: @post }
end
end
def new
@post = Post.new
render :new
end
def edit
@post = Post.find(params[:id])
render :edit
end
end
Rails 假设有一个与控制器同名的资源,并且自动神奇地填充控制器动作.它还假设 app/views/posts/(:action).html.[erb|haml|slim|jbuilder]
中有一个视图.这称为隐式渲染.
Rails assumes that there is a resource with the same name as the controller and auto-magically fills in the controller action. It also assumes there is a view in app/views/posts/(:action).html.[erb|haml|slim|jbuilder]
. This is known as implicit rendering.
评论大致显示了 rails 尝试的操作.
The comments show roughly what action rails attempts.
它不会填写对数据进行操作的操作(创建、更新、销毁),因为实际实现可能会有很大差异,并且很难做出有用的猜测.
It does not fill in actions which operate on data (create, update, destroy) since the actual implementation can vary greatly and it's hard to make useful guesses.
这篇关于Rails、响应块和 |格式|的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!