svoop
svoop

Reputation: 3454

Overrule previous redirect_to in controller

Given the following simplified situation (in reality, the scenario is from an ActiveAdmin backed app):

class ShapeController < ApplicationController
  def update
    (...)
    redirect_to
  end
end

class CircleController < ShapeController
  def update
    super
    (...)
    redirect_to
  end
end

Calling CircleController#update will cause the famous "AbstractController::DoubleRenderError" because redirect_to is called twice.

Now, I can't prevent the first call of redirect_to by super, at least not without messing with ActiveAdmin's code. Is there another way to cancel the first redirect_to and overrule it with another one?

Thanks for your hints!

Upvotes: 0

Views: 61

Answers (2)

nayiaw
nayiaw

Reputation: 1456

ActiveAdmin is using Inherited Resources to do perform the standard REST actions. The gem provided a way to overwrite the respond_to block. I've never try this before but this might be helpful in your case:

ActiveAdmin.register Circle do
  # ... 

  controller do
    def update
      update! do |success, failure|
        failure.html { redirect_to circle_url(@circle) }
      end
    end
  end

  # ...
end

Refer to the IR gem documentation for more options to overwrite the actions(under Overwriting actions section).

Upvotes: 1

Yury Lebedev
Yury Lebedev

Reputation: 4005

I would say it is not possible. The best solution would be to extract the action code in some protected controller method, and call it from the child controller:

class ShapeController < ApplicationController
  def update
    do_the_update
    redirect_to
  end

  protected

  def do_the_update
    # your code
  end
end

class CircleController < ShapeController
  def update
    do_the_update
    redirect_to
  end
end

Upvotes: 0

Related Questions