Hi, I have quetion about ''named route''. I defined the following in routes.rb to use '':title'' instead of '':id''. map.movie ''/ movies/:title'', :controller=>''movies'', :action=>''show'', :conditions=>{:method=>:get} This seems to work fine, but ''movie_path()'' still uses '':id'' instead of '':title''. movie = Movie.get(parms[:id]) p movie_path(movie) #=> "/movies/1" (expected is "/movies/Avatar") How to change ''movie_path()'' to use '':title''? -- regards, makoto -- 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.
you can use http://svn.techno-weenie.net/projects/plugins/permalink_fu/plugin On Wed, Apr 14, 2010 at 1:50 PM, makoto kuwata <kwatch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have quetion about ''named route''. > > I defined the following in routes.rb to use '':title'' instead of '':id''. > > map.movie ''/ > movies/:title'', :controller=>''movies'', :action=>''show'', > :conditions=>{:method=>:get} > > This seems to work fine, but ''movie_path()'' still uses '':id'' instead > of '':title''. > > movie = Movie.get(parms[:id]) > p movie_path(movie) #=> "/movies/1" (expected is "/movies/Avatar") > > How to change ''movie_path()'' to use '':title''? > > -- > regards, > makoto > > -- > 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.
> > I defined the following in routes.rb to use '':title'' instead of '':id''. > > map.movie ''/ > movies/:title'', :controller=>''movies'', :action=>''show'', > :conditions=>{:method=>:get} > > This seems to work fine, but ''movie_path()'' still uses '':id'' instead > of '':title''. > > movie = Movie.get(parms[:id]) > p movie_path(movie) #=> "/movies/1" (expected is "/movies/Avatar") > > How to change ''movie_path()'' to use '':title''? >The reason this happens is that movie_path converts an object to a parameter using the to_param method of the object, rather than knowing what parameter the route wants. So, there are two solutions: 1) class Movie def to_param name end end 2) movie_path(movie.title) Depending on how many other paths rely on you passing a movie around, that will affect your decision. A little known third option is this: class Movie def to_param "#{id}-#{name}" end end That way you can just use the param :id in your path (and 1-Avatar will be converted to the integer 1 when searched using the :id field) but the title of the movie will be in the URLs for SEO purposes. Cheers, Andy -- 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.
On 14 April 2010 09:29, bala kishore pulicherla <balumca21-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> you can use http://svn.techno-weenie.net/projects/plugins/permalink_fu/pluginI think you''re solving a different problem than he asked for though. Permalink_fu is good for creating slugs, so if he had "Throw Momma From The Train" as a movie it would stop URLs like this: "http://example.com/movies/Throw Momma From The Train" It''s a good point, using the title/name of the movie (that may have spaces) will break sooner or later, but he was asking about ids/names in parameters for named_routes. Cheers, Andy -- 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.
bala and Andy, Thank you very much! I choosed to override to_param() as "#{id}-#{name}". Thank you. -- regards, makoto On 4月14日, 午後5:33, Andy Jeffries <andyjeffr...@gmail.com> wrote:> > I defined the following in routes.rb to use '':title'' instead of '':id''. > > > map.movie ''/ > > movies/:title'', :controller=>''movies'', :action=>''show'', > > :conditions=>{:method=>:get} > > > This seems to work fine, but ''movie_path()'' still uses '':id'' instead > > of '':title''. > > > movie = Movie.get(parms[:id]) > > p movie_path(movie) #=> "/movies/1" (expected is "/movies/Avatar") > > > How to change ''movie_path()'' to use '':title''? > > The reason this happens is that movie_path converts an object to a parameter > using the to_param method of the object, rather than knowing what parameter > the route wants. > > So, there are two solutions: > > 1) > > class Movie > def to_param > name > end > end > > 2) > > movie_path(movie.title) > > Depending on how many other paths rely on you passing a movie around, that > will affect your decision. A little known third option is this: > > class Movie > def to_param > "#{id}-#{name}" > end > end > > That way you can just use the param :id in your path (and 1-Avatar will be > converted to the integer 1 when searched using the :id field) but the title > of the movie will be in the URLs for SEO purposes. > > Cheers, > > Andy-- 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.