嵌套控制器的创建操作respond_to无法呈现:new 如果保存失败



我有一个嵌套的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)

相关内容

最新更新