Rails ActiveAdmin:自定义密码重置动作跳转至自定义视图
Fixing the ActiveAdmin Password Reset Custom Action
Hey there! Let's get your password reset action working smoothly with the custom view and ActiveAdmin's default layout. Here's the corrected code and a breakdown of what was adjusted:
Corrected Full Code
ActiveAdmin.register User do # Register the custom route and define the action logic member_action :password_reset, method: :get do @user = User.find(params[:id]) # This will automatically render your custom view at app/views/active_admin/users/password_reset.html.erb end # Add the "Password reset" button to the user edit page action_item only: :edit do link_to "Password reset", admin_user_password_reset_path(resource) end end
Key Fixes & Explanations
- Swapped
action_viewforaction_item:action_itemis the proper ActiveAdmin method to add custom action buttons to resource pages. We useonly: :editto ensure the button only shows up on the user edit page. - Added
member_action: This registers the custom route for your password reset action (ActiveAuto generates the named routeadmin_user_password_reset_pathautomatically). We setmethod: :getsince we're just loading a view page. - Fixed
params[id]toparams[:id]: Ruby hash keys require colons for symbols—this was a syntax error that would have broken user lookup. - Used
resourcein the link:resourceis an ActiveAdmin helper that refers to the current user being edited, so we don't need to manually passuser.id(thoughadmin_user_password_reset_path(user)would also work).
Making the Custom View Work
Double-check your view file is saved at app/views/active_admin/users/password_reset.html.erb. Since the controller inherits from ActiveAdmin::BaseController, it will automatically use ActiveAdmin's default layout for this view—no extra setup needed! You can add your password reset form or instructions directly in this ERB file.
内容的提问来源于stack exchange,提问作者Karthik Muthu
相关产品推荐
相关产品推荐

