6

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 Exception for Album.find(). According to this I added:

  rescue_from Exception, with: :flash_error

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

I marked some parts above as _FIND_, _FLASH_, _DEST_ and I would like to go through all of them in that order. I tried to delete album that doesn't exists to trigger that. I got blank page with URL for albums/(:id) (the one I tried to delete) so I suppose I stuck at _FLASH_ part.

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

The main goal is to redirect to the correct page (specified at _DEST_), so maybe there's some better approach.

Community
  • 1
  • 1
pawel7318
  • 3,383
  • 2
  • 28
  • 44

1 Answers1

2

"rescue_from" callback method "flash_error" is behaving like a normal controller action that from user perspective ends up rendering blank page. In that sense it is not stuck there, it is done there.

In order to "proceed" you need to redirect or render. Notice that exception gets propagated so you can get more details about what happened:

 #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

My advice is never to catch all exceptions. In my experience nothing but grief ever came out of it. Imagine if you had exception in index action - that would end up in a loop.

drKreso
  • 1,030
  • 10
  • 16
  • Sounds good but as I have multiple actions that calls `set_album` (you can see that at `before_action`) and for every I have different `redirect_to` destination so I can't put just one `redirect_to` at `flash_error`. Is there a way to check what main action was called ? I could recall it from flash_error or make multiple `redirect_to` there with `case` – pawel7318 Apr 12 '14 at 10:55
  • You can get some info in exception.backtrace, but I am unsure why would you go to custom actions if something exceptional happend. What is recovery scenario if I want show action and no album is found? – drKreso Apr 12 '14 at 11:04
  • I rethought the whole thing and you're right. First of all it's exceptional so it shouldn't happen to often. Secondly I'll move to ajax in the future so there will be no page redirects or re-rendering required to just show a flash message for the user. I would appreciate If you may check [another question](http://stackoverflow.com/questions/23001377/rspec-feature-test-issue) I have as well. – pawel7318 Apr 12 '14 at 11:47