11

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
repié
  • 182
  • 2
  • 18
  • Check out this related question, the accepted answer is also the solution to your problem: http://stackoverflow.com/questions/7684644/activerecordreadonlyrecord-when-using-activeadmin-and-friendly-id – rdvdijk Oct 09 '11 at 21:50
  • I'll try that tonight, it is effectivelly due to the to_param command. I keep you on touch after a try. – repié Oct 10 '11 at 06:37

3 Answers3

25

There is a cleaner way to do this:

ActiveAdmin.register User do
 controller do
  defaults :finder => :find_by_slug
 end
end
Laura Popa
  • 456
  • 5
  • 6
13

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
afiah
  • 507
  • 5
  • 12
1

If you followed this railscast: http://railscasts.com/episodes/63-model-name-in-url-revised and have custom routes, you can fix the active_admin routes by placing this in the app/admin/user.rb:

before_filter :only => [:show, :edit, :update, :destroy] do
  @user = User.find_by_slug!(params[:id])
end

It's really close to the one shown by afiah, just slightly different.

lflores
  • 3,770
  • 3
  • 19
  • 24