Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

ActiveAdminで追加・更新後、一覧か編集画面へ遷移するようにする

More than 3 years have passed since last update.

ActiveAdminでモデルの追加・更新を行った後、デフォルトの挙動では閲覧画面へ遷移します。これを一覧か編集画面へ遷移したい場合があると思います。

一覧へ遷移したい場合(モデルクラスをMyModelとする)

app/admin/my_model.rb
ActiveAdmin.register MyModel do
  ~ もろもろ記述 ~

  controller do
    def create
      super do |success,failure|
        success.html {
          flash[:notice] = "Created."
          redirect_to collection_path 
        }
      end
    end

    def update
      super do |success,failure|        
        success.html {
          flash[:notice] = "Updated."
          redirect_to collection_path 
        }
      end
    end
  end
end

編集画面へ遷移したい場合

  controller do
    def create
      super do |success,failure|
        success.html { 
          flash[:notice] = "Created."
          redirect_to action: 'edit', id: MyModel.last.id 
        }
      end
    end

    def update
      super do |success,failure|
        success.html {
          flash[:notice] = "Updated."
          redirect_to action: 'edit' 
        }
      end
    end
  end

flash[:notice]はなくてもいいですが、メッセージ表示したほうがユーザーに親切だと思います。
もちろん

flash[:notice] = I18n.t("created")

のようにローカライズもできます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away