Hi all, Is it possible to implement REST with :name as a unique identifier of a model, instead of the :id? Having readable (literally) URLs is important to my application as many URLs are announced at conferences or read out over the phone. For example, I''d like to do: /branches /branches/bristol /branches/bristol;edit Cheers, Tom --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Apr-05 13:36 UTC
Re: REST with :name as unique identifier, rather than :id
On Apr 5, 2007, at 9:00 AM, Tom Taylor wrote:> Hi all, > > Is it possible to implement REST with :name as a unique identifier of > a model, instead of the :id? Having readable (literally) URLs is > important to my application as many URLs are announced at conferences > or read out over the phone. > > For example, I''d like to do: > > /branches > /branches/bristol > /branches/bristol;edit > > Cheers, > > TomIn your controller, you just need to replace: Branch.find(params[:id]) with: Branch.find_by_name(params[:id]) and then possibly add to your Branch model: class Branch def to_param self.name end end so URL generation will do the right thing if you have: edit_branch_url(@branch) or edit_branch_url(:id => @branch) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Taylor
2007-Apr-05 14:27 UTC
Re: REST with :name as unique identifier, rather than :id
On 5 Apr, 14:36, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> In your controller, you just need to replace: > > Branch.find(params[:id]) > > with: > > Branch.find_by_name(params[:id]) > > and then possibly add to your Branch model: > > class Branch > def to_param > self.name > end > end > > so URL generation will do the right thing if you have: > > edit_branch_url(@branch) > or > edit_branch_url(:id => @branch)Thanks Rob, That''s amazingly simple... Any other gotchas I should be aware of? Cheers, Tom --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---