LoginSignup
5
4

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-03-21

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")

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

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4