Sami Sami - 1 month ago 14
Ruby Question

Can't overwrite the active admin default redirect path

I am using active admin in my application. In my controller, I have an action update with

redirect_to
function. But while updating, it threw me an error.

Render and/or redirect were called multiple times in this action. Please note that you may only call render or redirect, and at most once per action. Also note that neither redirect nor render terminates execution of the action, so if you want to exit an action after redirecting, you need to do something like
redirect_to(...) and return
.

def update
@user = User.find(params[:id])
@user.update
mailers.notify(@user).deliver
redirect_to user_path(@user)
end


I tried


  1. only redirect_to

  2. redirect_to() and return



but nothing works.

before_filter :only =>[:create,:update] do
if self.action_name.to_sym == :create
@user = User.new(params[:user])
else
@user = User.find(params[:id])
end

Answer

What is your purpose? Why you use mailers.notify(@user).deliver inside the update action?

  1. Move the notification to after_update filter in your User model, smth like this:

    after_update :send_notifications
    
    def send_notifications 
      Mailer.notify(self).deliver
    end
    
  2. Change the update method to smth like this:

    def update
      super do |format|
        redirect_to user_path(@user), notice: "Your notice message" and return if resource.valid?
      end
    end
    
Comments