1
在"Working With Javascript" section of the Rails Edge Guides,給出一個例子如何構建一個「用戶」控制器內部有一個「創造」的行動,以AJAX融入創作行動:Rails Edge Guides,AJAX示例 - 爲什麼要同時使用'format.js'和'format.json'?
# app/controllers/users_controller.rb
# ......
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.js {}
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
我讀過here兩個'format.js'和'format.json'是需要的,因爲Javascript和JSON是不同類型的響應。我的問題是,如果兩種格式都是在「創建」行爲的if語句的前半部分中指定的,那麼爲什麼下半年不需要這兩種格式呢?這是否意味着Javascript在成功時使用'create.js.erb'文件,但不是失敗?