【发布时间】:2015-08-16 12:40:14
【问题描述】:
Rails scaffold 生成以下内容:
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
阅读this 后,我了解respond_to 是如何工作的(有点),但我不明白format 在做什么。不应该是 either format.html 或 format.json 而不是 both?这两行实际上在做什么?
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
里面有隐含的if吗?是不是有点像
if (format == html) {}
if (format == json) {}
旁注:为什么update 需要respond_to 块,而show 将处理/students/1.json 或/students/1 根本没有任何逻辑?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4