Hi, I''m pretty new to the concept of restful routing and am struggling to find any rails 2 info that helps with what I''m looking for. I have a regular scaffold setup and the routes are setup as default map.resources :pages I would like to be able to use titles in the url rather than relying on the id. Eg localhost:3000/pages/sometitle instead of just localhost:3000/pages/1 Can anyone help me out? Thanks -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
In your model you can override the to_param method like so def to_param "#{self.id}-#{self.sometitle.downcase.gsub(/[^a-z0-9]+/i, ''-'')}" end and you''ll get a route like localhost:3000/pages/1-sometitle replacing sometitle with an attribute of your model. John On Aug 27, 2:40 pm, Dan Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, I''m pretty new to the concept of restful routing and am struggling > to find any rails 2 info that helps with what I''m looking for. > > I have a regular scaffold setup and the routes are setup as default > > map.resources :pages > > I would like to be able to use titles in the url rather than relying on > the id. > Eg > localhost:3000/pages/sometitle > > instead of just > > localhost:3000/pages/1 > > Can anyone help me out? > > Thanks > -- > Posted viahttp://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
oops... you really don''t need to specify ''self'' in there, just ''id'' and ''sometitle'' will do. On Aug 27, 2:49 pm, John Yerhot <joyer...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In your model you can override the to_param method like so > > def to_param > "#{self.id}-#{self.sometitle.downcase.gsub(/[^a-z0-9]+/i, ''-'')}" > end > > and you''ll get a route like > localhost:3000/pages/1-sometitle > > replacing sometitle with an attribute of your model. > > John > > On Aug 27, 2:40 pm, Dan Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hi, I''m pretty new to the concept of restful routing and am struggling > > to find any rails 2 info that helps with what I''m looking for. > > > I have a regular scaffold setup and the routes are setup as default > > > map.resources :pages > > > I would like to be able to use titles in the url rather than relying on > > the id. > > Eg > > localhost:3000/pages/sometitle > > > instead of just > > > localhost:3000/pages/1 > > > Can anyone help me out? > > > Thanks > > -- > > Posted viahttp://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Hi Dan, I''m new to Rails myself, but I don''t think you have a restful issue here. I''ll make a couple of assumptions: You are calling the new page using a link_to helper and you are calling the show action. In your link_to, you probably see a restful named route called something like pages_path(title). The sometitle is an instance variable holding the ID field. You simply want to replace that with title.sometitle. That will provide you the url you want. But, that is only the first half. The receiving action, presumably show, expects the ID, something like: Pages.find(params[:id]). You will need to change that to Pages.find_by_title(params[:title] [:sometitle], or something similar. This assumes title is a field in the table pages. Hope this helps. On Aug 27, 3:40 pm, Dan Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, I''m pretty new to the concept of restful routing and am struggling > to find any rails 2 info that helps with what I''m looking for. > > I have a regular scaffold setup and the routes are setup as default > > map.resources :pages > > I would like to be able to use titles in the url rather than relying on > the id. > Eg > localhost:3000/pages/sometitle > > instead of just > > localhost:3000/pages/1 > > Can anyone help me out? > > Thanks > -- > Posted viahttp://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Thanks guys, I''ll save this thread and refer to it again. I find a lot of things in Rails seem to take me a lot longer to understand than they should:) -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On Aug 27, 3:55 pm, Dan Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks guys, I''ll save this thread and refer to it again. I find a lot > of things in Rails seem to take me a lot longer to understand than they > should:) > -- > Posted viahttp://www.ruby-forum.com/.It''s great that you''re already trying to adhere to RESTful principles, though! Many newcomers to Rails don''t start that way and end up feeling the pain later. Jeff REST with Rails Oct 4, 2008, in Austin, TX $295 (get 10% off with discount code "PWRRGG") http://www.purpleworkshops.com/workshops/rest-and-web-services --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rsl''s stringex library also does the same thing: http://github.com/rsl/stringex/tree/master On 28/08/2008, at 6:29 AM, Jeff wrote:> > On Aug 27, 3:55 pm, Dan Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Thanks guys, I''ll save this thread and refer to it again. I find a >> lot >> of things in Rails seem to take me a lot longer to understand than >> they >> should:) >> -- >> Posted viahttp://www.ruby-forum.com/. > > It''s great that you''re already trying to adhere to RESTful principles, > though! Many newcomers to Rails don''t start that way and end up > feeling the pain later. > > Jeff > > REST with Rails > Oct 4, 2008, in Austin, TX > $295 (get 10% off with discount code "PWRRGG") > http://www.purpleworkshops.com/workshops/rest-and-web-services > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---