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

James Chevalier
  • 10,604
  • 5
  • 48
  • 74
Marcelo
  • 1,702
  • 2
  • 24
  • 42

6 Answers6

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
nazar kuliyev
  • 1,215
  • 1
  • 12
  • 13
  • I am getting this error: development/myproject/app/admin/features.rb:1:in `': undefined method `controller' for main:Object (NoMethodError) what I am doing wrong? – Abdul Baig Oct 28 '16 at 11:43
  • it should be placed inside ActiveAdmin.register ModelName do end block – nazar kuliyev Dec 04 '16 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 02 '18 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.

kravc
  • 583
  • 5
  • 15
  • 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 01 '13 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 }` – Alter Lagos May 23 '13 at 16:07
  • Thank you guys. Im sorry for the late confirmation. – Marcelo Jun 26 '13 at 14:19
  • 1
    this method actually ignores validation errors (maybe in latest active_admin versions), I have posted code that works for me – nazar kuliyev Mar 18 '14 at 17:02
  • I am getting this error: development/myproject/app/admin/features.rb:1:in `': undefined method `controller' for main:Object (NoMethodError) what I am doing wrong? – Abdul Baig Oct 28 '16 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  
Anri
  • 6,175
  • 3
  • 37
  • 61
  • I am getting this error: development/myproject/app/admin/features.rb:1:in `': undefined method `controller' for main:Object (NoMethodError) what I am doing wrong? – Abdul Baig Oct 28 '16 at 11:43
  • I'm sorry, the post is from 2014, I haven't used ruby for 2 years now. – Anri Oct 31 '16 at 09: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
Jasper
  • 1,971
  • 19
  • 34
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
rorofromfrance
  • 1,854
  • 12
  • 21
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.

James Chevalier
  • 10,604
  • 5
  • 48
  • 74
kakubei
  • 5,321
  • 4
  • 44
  • 66