How to put multiple lines of code in a format.html block?

My controller

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

end

I need this request from html, then it redirects to: back, like

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

It works fine when I can't handle json. I want to combine both of them so that they send a response according to the html or ajax request

+4
source share
2 answers

You can just put it inside the response_to block for the html format

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
+5
source

You can put several lines in a block.

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

Source: https://habr.com/ru/post/1611398/


All Articles