Aurélien Malisart
2007-Jul-05 16:40 UTC
referencing REST resources with something other than ids
Hello. I got a REST URL personalization issue... Here is an example to explain: If have two nested resources set like this in "routes.rb": map.resources :teams do |teams| teams.resources :players end So, some teams will be accessible with URL such that: /teams/42 /teams/42/edit and the players by some of the form: /team/42/players /team/42/players/18 /team/42/players/new etc... Is there a convenient way do not use the "id" column of the "team" and "player" models but another one (e.g. "name", supposing it''s unique) to have nicer URLs but keeping the ability to use *_path helpers? So, I dream of URLs such that, keeping the same example: /teams/mylovelyteam /teams/mylovelyteam/players /teams/mylovelyteam/players/new /teams/mylovelyteam/players/theleader I hope my explanation is understandable... Thanks. aurels, belgium --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Daniel N
2007-Jul-05 16:52 UTC
Re: referencing REST resources with something other than ids
On 7/6/07, Aurélien Malisart <aurelien.malisart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hello. > I got a REST URL personalization issue... Here is an example to > explain: > > If have two nested resources set like this in "routes.rb": > > map.resources :teams do |teams| > teams.resources :players > end > > So, some teams will be accessible with URL such that: > > /teams/42 > /teams/42/edit > > and the players by some of the form: > > /team/42/players > /team/42/players/18 > /team/42/players/new > > etc... > > Is there a convenient way do not use the "id" column of the "team" and > "player" models but another one (e.g. "name", supposing it''s unique) > to have nicer URLs but keeping the ability to use *_path helpers? > So, I dream of URLs such that, keeping the same example: > > /teams/mylovelyteam > /teams/mylovelyteam/players > /teams/mylovelyteam/players/new > /teams/mylovelyteam/players/theleader > > I hope my explanation is understandable... > > Thanks. > > aurels, belgiumI know of one way. I can''''t say if it''s the best or not. When a route is generated, the id method of the object is not actually called. It''s just the default. The magic method is to_param So in your method if you put def to_param self.name end Then this should give you the routes that your after. You will need to escape it though so it doesn''t give you bad urls. However.... I believe that by doing this ( I haven''t tried it myself ) it will still call the parameter :id, but you will need to find_by_name in your controllers. so was ==> Team.find( params[:id] ) Now ==> Team.find_by_name( params[:id] ) The way I have done it to avoid this is to include the id still. But I don''t know if I''ll keep it like this for ever. /team/1-my-lovely-team This way ruby still interprets the id as an integer and I can use it as normal. Here''s the code I''m using. I got the escaping code from Rick Olson''s permalink_fu plugin cattr_reader :translation_to, :translation_from @@translation_to = ''ascii//ignore//translit'' @@translation_from = ''utf-8'' # Ripped directly from permalink_fu by Rick Olson def escape_slug(str) s = Iconv.iconv(self.class.translation_to, self.class.translation_from, str).to_s s.gsub!(/\W+/, '' '') # all non-word chars to spaces s.strip! # ohh la la s.downcase! # s.gsub!(/\ +/, ''-'') # spaces to dashes, preferred separator char everywhere s end def to_param escape_slug "#{self.id}-#{self.name}" end HTH Daniel --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aurélien Malisart
2007-Jul-05 17:15 UTC
Re: referencing REST resources with something other than ids
The first solution works good ;) So thanks! Hope this will help some else! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jon Garvin
2007-Jul-05 17:25 UTC
Re: referencing REST resources with something other than ids
Aurélien Malisart wrote:> Hello. > I got a REST URL personalization issue... Here is an example to > explain: > > If have two nested resources set like this in "routes.rb": > > map.resources :teams do |teams| > teams.resources :players > end > > So, some teams will be accessible with URL such that: > > /teams/42 > /teams/42/edit > > and the players by some of the form: > > /team/42/players > /team/42/players/18 > /team/42/players/new > > etc... > > Is there a convenient way do not use the "id" column of the "team" and > "player" models but another one (e.g. "name", supposing it''s unique) > to have nicer URLs but keeping the ability to use *_path helpers? > So, I dream of URLs such that, keeping the same example: > > /teams/mylovelyteam > /teams/mylovelyteam/players > /teams/mylovelyteam/players/new > /teams/mylovelyteam/players/theleader > > I hope my explanation is understandable... > > Thanks. > > aurels, belgium >Yes, sort of. I''ve got a project that does this with one of it''s controllers and it works great. What you need to understand is that the RESTful routes will just pass whatever comes after /teams/ in the url as the :id in the hash. So, what you would do in your controller is... Team.find_by_name(params[:id]) This then assumes that you''ll always, always, always get the team name in the url, and will never be trying to call /teams/42, as that would just look for a team named "42", which may or may not exist. One option is, if you''re certain you''ll NEVER have a team who''s name is a number, then you could do a REGEX check on the params[:id] and if it purely numbers, then do a normal find by :id, otherwise lookup by name with the params[:id] that isn''t really an :id. -- http://www.5valleys.com/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Jul-05 18:34 UTC
Re: referencing REST resources with something other than ids
> Here''s the code I''m using. I got the escaping code from Rick Olson''s > permalink_fu pluginYou can, of course, just install the plugin :) http://svn.techno-weenie.net/projects/plugins/permalink_fu/README -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Daniel N
2007-Jul-06 00:46 UTC
Re: referencing REST resources with something other than ids
On 7/6/07, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > Here''s the code I''m using. I got the escaping code from Rick Olson''s > > permalink_fu plugin > > You can, of course, just install the plugin :) > > http://svn.techno-weenie.net/projects/plugins/permalink_fu/README > > -- > Rick Olson > http://lighthouseapp.com > http://weblog.techno-weenie.net > http://mephistoblog.comRick, Can this be used for the purpose outlined previously, just to provide a slug, or do I need to use it with the date prefix, and a seperate db column? Cheers Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Jul-06 01:14 UTC
Re: referencing REST resources with something other than ids
> Can this be used for the purpose outlined previously, just to provide a > slug, or do I need to use it with the date prefix, and a seperate db column?The db column is so you''re not constantly escaping your titles. It also ensures that the permalink doesn''t change each time you tweak the title, which can be bad for SEO. Still, you can use PermalinkFu.escape to manually escape in a #to_param method if you want. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Daniel N
2007-Jul-06 01:23 UTC
Re: referencing REST resources with something other than ids
On 7/6/07, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > Can this be used for the purpose outlined previously, just to provide a > > slug, or do I need to use it with the date prefix, and a seperate db > column? > > The db column is so you''re not constantly escaping your titles. It > also ensures that the permalink doesn''t change each time you tweak the > title, which can be bad for SEO. Still, you can use > PermalinkFu.escape to manually escape in a #to_param method if you > want. > > -- > Rick Olson > http://lighthouseapp.com > http://weblog.techno-weenie.net > http://mephistoblog.comJust so I understand, I don''t want to be a pain in the arse. With the way that I''m doing it if the title changes it doesn''t matter to the app, since the id is at the start of the slug, is this still bad for SEO since the link that the SE has is still valid? Do I need to have the date prefix /blah/2007/07/07/permalink_fu Cheers Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Aurélien Malisart
2007-Jul-06 14:16 UTC
Re: referencing REST resources with something other than ids
> What you need to understand is that the RESTful routes > will just pass whatever comes after /teams/ in the url as the :id in the hashI do ;-)> So, what you would do in your controller is... > > Team.find_by_name(params[:id])That''s what I''m doing actually. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---