user2369480
user2369480

Reputation: 71

Active_admin double render error message if i try return false or redirect to in update action

I got message AbstractController::DoubleRenderError in Admin::AdminsController#update

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 terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return"

also redirect_to(...) and return not helped :(

How to fix that my problem? Help me, please? I want render error flash message and not update all stuff after if check_card_enabled? if in If return true. Thanks!

def update_resource(object, attributes)
  if check_card_enabled?
    redirect_to admin_admin_path(@admin)
    flash[:alert] = I18n.t('errors.messages.please_add_gateway_info')
    return false
  end
  update_method = if attributes.first[:password] == attributes.first[:password_confirmation] &&
      attributes.first[:password].present? || attributes.first[:password_confirmation].present?
                    :update_attributes
                  else
                    :update_without_password
                  end
  object.send(update_method, *attributes)
end

def check_card_enabled?
  @admin.card_disabled? && (params[:admin][:card_disabled] == '0') && @admin.gateway_info.nil?
end

Anser:

before_filter :check_card_enabled, only: :update

def check_card_enabled
  admin = Admin.find_by(email: params[:admin][:email])
  if admin.card_disabled? && (params[:admin][:card_disabled] == '0') && admin.gateway_info.nil?
    flash[:alert] = I18n.t('errors.messages.please_add_gateway_info')
    redirect_to edit_admin_admin_url(admin)
  end
end

Upvotes: 1

Views: 855

Answers (1)

code_aks
code_aks

Reputation: 2074

Yes, you need to return from method when doing redirect. It actually only adds appropriate headers for the response object.

You can write more rubyish way:

if some_condition
    return redirect_to(path_one)
end

so according to your code try :-

def update_resource(object, attributes)
  if check_card_enabled?
    flash[:alert] = I18n.t('errors.messages.please_add_gateway_info')
    return redirect_to admin_admin_path(@admin), 
  end

 ....
 ....

Upvotes: 1

Related Questions