我有一个嵌套的Photo
控制器,具有以下操作:
def create
@photo = @gallery.photos.new(photo_params)
if @photo.save
flash[:notice] = "Created new photo"
else
flash[:error] = "Couldn't create photo"
end
respond_with [@gallery, @photo]
end
如果照片参数是有效的,它正确地重定向到gallery_photo_path
,但是如果参数无效并且@photo不能成功保存,而不是像我期望的那样渲染:new
模板,它重定向到不存在的/galleries/1/photos
。
如果我不使用respond_with
和硬编码渲染,一切都很好:
def create
@photo = @gallery.photos.new(photo_params)
if @photo.save
flash[:notice] = "Created new photo"
redirect_to gallery_photo_path @gallery, @photo
else
flash[:error] = "Couldn't create photo"
render :new
end
end
为什么respond_to
不能呈现新的模板时,保存文件概述在文档?
我在控制器中使用respond_to :html
,所有其他动作都按预期运行。
文档声明使用respond_with
与对象作为参数,而不是与显式数组,所以
respond_with [@gallery, @photo]
应该respond_with(@gallery, @photo)