19

I have a Feature page that belongs to the Car page. That is working exactly how I want to, except for one thing.

After creating, updating or destroying, I want the page to be redirected to the admin_car_path(car) instead of the defaults admin_car_feature_path(car,feature) for create and update and admin_car_features_path(car).

I unsuccessfully searched for that.

ActiveAdmin.register Car do
end

ActiveAdmin.register Feature do
  belongs_to :car
end

TIA

6 Answers 6

48

right code for updating without skipping validation

controller do
  def update
    super do |success,failure|
      success.html { redirect_to collection_path }
    end
  end
end
3
  • I am getting this error: development/myproject/app/admin/features.rb:1:in <top (required)>': undefined method controller' for main:Object (NoMethodError) what I am doing wrong?
    – Abdul Baig
    Oct 28, 2016 at 11:43
  • it should be placed inside ActiveAdmin.register ModelName do end block Dec 4, 2016 at 11:24
  • 4
    It's not recommended to use the super keywords in AA (silly, I know. DSL overkill IMO). that's essentially what the update! method is for. You can also do update! do |success, failure|
    – Nick Res
    Aug 2, 2018 at 19:09
28

Here is the code for update action for your case. This code goes to the features.rb - admin file:

controller do
  def update
    update! do |format|
      format.html { redirect_to admin_cars_path }
    end
  end
end

This redirects to the cars index page. So you have the idea. Same for create and destroy actions.

5
  • another good thing about this approach: it does not override validation errors being shown on edit page when the object is invalid (as I feared it might since the block doesn't distinguish whether valid or not).
    – Luke W
    Feb 1, 2013 at 17:23
  • 4
    Instead of format.html { redirect_to admin_cars_path } you could use the generic approach format.html { redirect_to collection_path } May 23, 2013 at 16:07
  • Thank you guys. Im sorry for the late confirmation.
    – Marcelo
    Jun 26, 2013 at 14:19
  • 1
    this method actually ignores validation errors (maybe in latest active_admin versions), I have posted code that works for me Mar 18, 2014 at 17:02
  • I am getting this error: development/myproject/app/admin/features.rb:1:in <top (required)>': undefined method controller' for main:Object (NoMethodError) what I am doing wrong?
    – Abdul Baig
    Oct 28, 2016 at 11:43
13

At the current moment accepted answer leads to ignoring validation errors.

This works for me with the latest versions of ActiveAdmin and Rails:

controller do

  def update
    update! do |format|
      format.html { redirect_to collection_path } if resource.valid?
    end
  end

  def create
    create! do |format|
      format.html { redirect_to collection_path } if resource.valid?
    end
  end

end  
2
  • I am getting this error: development/myproject/app/admin/features.rb:1:in <top (required)>': undefined method controller' for main:Object (NoMethodError) what I am doing wrong?
    – Abdul Baig
    Oct 28, 2016 at 11:43
  • I'm sorry, the post is from 2014, I haven't used ruby for 2 years now.
    – Anri
    Oct 31, 2016 at 9:17
3

Here is a solution that also works with create_another, using parent and child for model names.

This solution assumes that you show children as part of parent (e.g. via table_for) so you do not need child's index method.

In resource override controller's smart_resource_url and index methods:

  controller do
    def smart_resource_url
      if create_another?
        new_resource_url(create_another: params[:create_another])
      else
        parent_path(params[:parent_id])
      end
    end

    def index
      redirect_to parent_path(params[:parent_id])
    end
  end
1

Current answer is skipping validations. Some of the other answers are working but partially correct (incorrect use of super or manually validating resource).

Most updated "proper" way to redirect with AA after create and udpate:

controller do
  def create
    create! do |success,failure|
      success.html { redirect_to collection_path, notice: "#{resource.model_name.human} was successfully created." }
    end
  end
  def update
    update! do |success,failure|
      success.html { redirect_to collection_path, notice: "#{resource.model_name.human} was successfully updated." }
    end
  end
end
0

Marcelo, I'm not sure I understand your question, but wouldn't putting this into the update, create and destroy actions in your controller do the trick?

 format.html { redirect_to redirect_address }

And make redirect_address whatever you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.