Catch all exceptions in rails before_action controller

albums_controller.rb:

lass AlbumsController < ApplicationController
  before_action :set_album, only: [:show, :edit, :update, :destroy]

  def destroy
    if @album.destroy
      redirect_to albums_url, notice: 'Album was successfully destroyed.'
    else
      redirect_to albums_url, error: 'Album destroy failed.' # _DEST_
    end
  end

 private
    def set_album
      @album = Album.find(params[:id]) # _FIND_
    end
end

I would like to catch an exception for Album.find(). Accordingly , I added:

  rescue_from Exception, with: :flash_error

  # private
  def flash_error
      flash_message :error, 'Something went wrong..' # _FLASH_
  end

I noted some of the above as _FIND_, _FLASH_, _DEST_, and I would like to go through all of them in this order. I tried to delete an album that does not exist to run it. I got a blank page with the url for albums / (: id) (the one I was trying to remove), so I suppose I'm stuck in parts _FLASH_.

What should I do to call destroyaction (I mean the original, called as rescue_form, because it can catch other exceptions for other controller actions). And how to get a better message than Something went wrong?

- ( _DEST_), , , .

+4
1

"rescue_from" "flash_error" , . , .

"", . , , , :

 #I am using Rails 3.2 flash notation
 def flash_error(exception)
    flash[:error] = "#{exception.message} (Something went wrong..)" # _FLASH_
    redirect_to albums_url
 end

. . , - .

+1

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


All Articles