Module: RedirectHelper

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/controllers/concerns/redirect_helper.rb

Instance Method Summary collapse

Instance Method Details

#destroy_redirect(object, **args) ⇒ Object

Parameters:

  • object (Object)

    an ActiveRecord model instance



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/concerns/redirect_helper.rb', line 6

def destroy_redirect(object, **args)
  back_location = (request.referer || root_path)

  unless object.persisted?
    begin
      back_params = Rails.application.routes.recognize_path(back_location)
      show_params = { controller: controller_name, action: 'show', id: object.id.to_s }

      # NOTE: url_for resolves to index action if object is not persisted
      back_location = url_for(object.metamorphosize) if back_params == show_params
    rescue; end
  end

  redirect_to(back_location, args)
end