Hi, Could anyone help with this one. I want to have two routes into my news stories for the show view, one by ID and the other by the URL of the story which is stored in the db. I have two routes setup ... map.connect ''itcontractormortgagenews/:id'', :controller => ''posts'', :action => ''show'' map.connect ''itcontractormortgagenews/:url'', :controller => ''posts'', :action => ''show'' and I have edited my show method in the controller to ... if params[:id].to_i > 0 @post = Post.find(params[:id]) else @post = Post.find_by_sql("select * from posts where URL = ''" + params[:url] + "''").first end The mapping by URL works fine but by ID not. I''m sure there is a better way to do this. Thanks Richard. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
use the requirement attribute in the routes and use friendly_id gem, http://railscasts.com/episodes/70-custom-routes this shows how the use requirement in routes , remember to put specific routes on top and more generic routes below with frienly_id gem you just have to config.gem and add has_friendly_id :url in your model , the gem will take care of finding the record and will put the given field in the url query string On Thu, Sep 9, 2010 at 6:14 AM, Creative Technologist < richard-DXT9u3ndKiRtdF/iL03xVL6Z9KU14Uj5tUK59QYPAWc@public.gmane.org> wrote:> Hi, > > Could anyone help with this one. > > I want to have two routes into my news stories for the show view, one > by ID and the other by the URL of the story which is stored in the db. > > I have two routes setup ... > > map.connect ''itcontractormortgagenews/:id'', :controller => > ''posts'', :action => ''show'' > map.connect ''itcontractormortgagenews/:url'', :controller => > ''posts'', :action => ''show'' > > and I have edited my show method in the controller to ... > > if params[:id].to_i > 0 > @post = Post.find(params[:id]) > else > @post = Post.find_by_sql("select * from posts where URL = ''" + > params[:url] + "''").first > end > > The mapping by URL works fine but by ID not. I''m sure there is a > better way to do this. > > Thanks > > Richard. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Perfect, good video link, thanks radhames! On Sep 9, 1:52 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> use the requirement attribute in the routes and use friendly_id gem, > > http://railscasts.com/episodes/70-custom-routes > > this shows how the use requirement in routes , remember to put specific > routes on top and more generic routes below > > with frienly_id gem you just have to config.gem and add > > has_friendly_id :url in your model , the gem will take care of finding > the record and will put the given field in the url query string > > On Thu, Sep 9, 2010 at 6:14 AM, Creative Technologist < > > rich...-DXT9u3ndKiRtdF/iL03xVL6Z9KU14Uj5tUK59QYPAWc@public.gmane.org> wrote: > > Hi, > > > Could anyone help with this one. > > > I want to have two routes into my news stories for the show view, one > > by ID and the other by the URL of the story which is stored in the db. > > > I have two routes setup ... > > > map.connect ''itcontractormortgagenews/:id'', :controller => > > ''posts'', :action => ''show'' > > map.connect ''itcontractormortgagenews/:url'', :controller => > > ''posts'', :action => ''show'' > > > and I have edited my show method in the controller to ... > > > if params[:id].to_i > 0 > > @post = Post.find(params[:id]) > > else > > @post = Post.find_by_sql("select * from posts where URL = ''" + > > params[:url] + "''").first > > end > > > The mapping by URL works fine but by ID not. I''m sure there is a > > better way to do this. > > > Thanks > > > Richard. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.