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开发者_JS百科 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.
There is a cleaner way to do this:
ActiveAdmin.register User do
controller do
defaults :finder => :find_by_slug
end
end
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
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.
精彩评论