Karthik Muthu
Karthik Muthu

Reputation: 135

Rails activeadmin redirect to customized view page from customse action

I have integrate active-admin in one of my rails application. Inside users edit page, I have enabled a custom action which is named as Password reset. When I click this action it have to take me to the custom view page which is located inside /views/active_admin/users/password_reset.html.erb with default active-admin layout.

ActiveAdmin.register User do

  action_view only: :edit do
    link_to "Reset", path(id: user.id)
  end

  controller do

    def password_reset
      @user = User.find(params[id])
    end

  end
end

Upvotes: 1

Views: 2575

Answers (2)

Wasif Hossain
Wasif Hossain

Reputation: 3940

You may try this:

ActiveAdmin.register User do
  action_item :password_reset_action_item, only: :edit do
    link_to 'Reset', password_reset_admin_user_path(id: user.id)
  end

  member_action :password_reset do
    @user = User.find(params[id]) # tips: you can (& should) use `resource` to lookup records by id. it will save you a couple of lines when you integrate authorization adapters like cancancan/pundit
    render 'active_admin/users/password_reset' # you may omit this line if your template and action names are equal
  end
end

Upvotes: 3

Pardeep Saini
Pardeep Saini

Reputation: 2102

You can user collection_action for this. A collection action is a controller action which operates on the collection of resources. This method adds both the action to the controller as well as generating a route for you.

collection_action :password_reset, method: :get do
  # Do some work here... 
end

and create view at view/admin/users/password_reset.html.erb

You can get view with default active-admin layout.

Upvotes: 1

Related Questions