如何将多行代码放入format.html块?

时间:2015-10-13 08:50:59

标签: ruby-on-rails ruby json

我的控制器是

def destroy
@image.destroy
respond_to do |format|
  format.html
  format.json { render json: 'success' }
end

我希望来自html的请求然后重定向到:像

一样
flash[:notice] = "Image Successfully deleted"
redirect_to :back

当我无法处理json时,它工作正常。我想将它们结合起来,以便相应地发送响应html或ajax请求

2 个答案:

答案 0 :(得分:5)

您可以将其放在html格式的respond_to块中

def destroy
  @image.destroy
  respond_to do |format|
    format.html do
      flash[:notice] = "Image Successfully deleted"
      redirect_to :back
    end
    format.json do
      render json: 'success'
    end
  end
end

答案 1 :(得分:3)

您可以将多行放入一个块中。

respond_to do |format|
  format.html do
    flash[:notice] = "Image Successfully deleted"
    redirect_to :back
  end
  format.json { render json: 'success' }
end