Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to custom ActiveAdmin using find_by request instead of ID for all actions

I'm just adding ActiveAdmin to my app, I got a problem using show/edit/destroy action cause my link doesn't point to ID but to users name (in order to be more readable for user).

ActiveAdmin correctly create my link like:

edit link: http://localhost:3000/admin/users/paul/edit (where paul is the user name)

in that case I get:

Couldn't find User with ID=paul

cause of course Paul is not the id but the user name.

How can I custom ActiveAdmin to use find_by_name(params[:id]) like in my application for all the action show/edit/delete?

In other model I got a so called "SID" which is a generated salted ID and I would like to use also the find_by_sid(params[:id]) as well for other models.

like image 330
repié Avatar asked Oct 09 '11 17:10

repié


2 Answers

There is a cleaner way to do this:

ActiveAdmin.register User do
 controller do
  defaults :finder => :find_by_slug
 end
end
like image 83
Laura Popa Avatar answered Nov 01 '22 01:11

Laura Popa


This will do the job in the app/admin/user.rb :

ActiveAdmin.register User do
    before_filter :only => [:show, :edit, :update, :destroy] do
        @user = User.find_by_name(params[:id])
      end
end
like image 43
afiah Avatar answered Nov 01 '22 02:11

afiah