【发布时间】:2017-05-18 00:35:49
【问题描述】:
所以我一直在浏览 Rails Zombies 并进入了解释 format.html 和 .json 的部分 我的问题是这些代码行做什么,为什么我们有它们?如果我在没有这些格式代码的情况下编写这些方法或操作,它们工作得非常好,因为我假设它们只是默认以 html 格式显示?如果有人能准确地弄清楚这段代码是做什么的,我将不胜感激,我也不完全理解 JSON 是什么。
def create
@zombie = Zombie.new(zombie_params)
respond_to do |format|
if @zombie.save
format.html { redirect_to @zombie, notice: 'Zombie was successfully created.' }
format.json { render :show, status: :created, location: @zombie }
else
format.html { render :new }
format.json { render json: @zombie.errors, status: :unprocessable_entity }
end
end
【问题讨论】:
标签: html ruby-on-rails json