Hi, If I am making an application that has a programmes model which has many episodes, and use nested resourses like so: map.resources :programmes do |programmes| programmes.resources :episodes end This then produces urls like so: /programmes/4/episodes/5 If I then use permalins, I can get them to look like this: /programmes/simpsons/episodes/bart-the-general Is there any way of removing the references to the models in the url? So the url would look like: /simpsons/bart-the-general Thanks, DAZ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Don''t use the built-in RESTful routing. :) Or define your own custom routes: map.connect '':show_name/:episode_id'', :controller => ''episodes'', :action => ''show'' I''m not sure that''s a valid route, but it would look something like that. In theory, that would minimize the amount of code you''d need to change in your controllers. --Jeremy On 8/20/07, DAZ <daz4126-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, If I am making an application that has a programmes model which > has many episodes, and use nested resourses like so: > > map.resources :programmes do |programmes| > programmes.resources :episodes > end > > This then produces urls like so: > > /programmes/4/episodes/5 > > If I then use permalins, I can get them to look like this: > > /programmes/simpsons/episodes/bart-the-general > > > Is there any way of removing the references to the models in the url? > So the url would look like: > > /simpsons/bart-the-general > > Thanks, > > DAZ > > > > >-- http://www.jeremymcanally.com/ My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Aug-20 15:12 UTC
Re: Simplifying RESTful urls
Hi -- On Mon, 20 Aug 2007, Jeremy McAnally wrote:> > On 8/20/07, DAZ <daz4126-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> Hi, If I am making an application that has a programmes model which >> has many episodes, and use nested resourses like so: >> >> map.resources :programmes do |programmes| >> programmes.resources :episodes >> end >> >> This then produces urls like so: >> >> /programmes/4/episodes/5 >> >> If I then use permalins, I can get them to look like this: >> >> /programmes/simpsons/episodes/bart-the-general >> >> >> Is there any way of removing the references to the models in the url? >> So the url would look like: >> >> /simpsons/bart-the-general >> > > Don''t use the built-in RESTful routing. :) Or define your own custom routes: > > map.connect '':show_name/:episode_id'', :controller => ''episodes'', > :action => ''show'' > > I''m not sure that''s a valid route, but it would look something like > that. In theory, that would minimize the amount of code you''d need to > change in your controllers.It''s valid route, but it''s a bit "greedy", in the sense that if you also had: map.connect '':actor_name/:movie_id'' or something, you''d have two '':wildcard/:wildcard'' routes, of which the first to be defined would "win" for all ''x/y'' URLs. This may or may not become a problem in a given application, but I''ve found that it does tend to become one :-) You could tweak it with regex matching for the various wildcards, if that''s appropriate. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
Not only that, but the routes that include the resource names might be
very handy with respect to SEO. That is, you might get higher on a
list for someone searching for a "Simpsons episode..." if your url
includes both.
If using the name, rather than the id, is something that you can
commit to as a universal design decision for your urls then there''s a
pretty easy way to make the pretty urls without changing the routing.
ActiveRecord objects make use of the ''to_param'' method when
they are
used as parameters to a method call. By default they return the
record id. That''s why the call:
episode_path(@simpsons, @bart_the_general)
comes out like /programmes/4/episodes/5
If you simply override the method then you can use the same routing
but the names instead:
class Programme
...
def to_param
self.name.dasherize
end
end
class Episode
...
def to_param
self.name.dasherize
end
end
(Having written that twice I see a refactoring coming on... :)
Now your call to episode_path should produce:
/programmes/simpsons/episode/bart-the-general
The remaining work to be done is to update your controllers, making
them capable of finding the objects based on the dasherized name.
HTH,
AndyV
On Aug 20, 11:12 am, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
wrote:> Hi --
>
>
>
> On Mon, 20 Aug 2007, Jeremy McAnally wrote:
>
> > On 8/20/07, DAZ
<daz4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> >> Hi, If I am making an application that has a programmes model
which
> >> has many episodes, and use nested resourses like so:
>
> >> map.resources :programmes do |programmes|
> >> programmes.resources :episodes
> >> end
>
> >> This then produces urls like so:
>
> >> /programmes/4/episodes/5
>
> >> If I then use permalins, I can get them to look like this:
>
> >> /programmes/simpsons/episodes/bart-the-general
>
> >> Is there any way of removing the references to the models in the
url?
> >> So the url would look like:
>
> >> /simpsons/bart-the-general
>
> > Don''t use the built-in RESTful routing. :) Or define your
own custom routes:
>
> > map.connect '':show_name/:episode_id'', :controller
=> ''episodes'',
> > :action => ''show''
>
> > I''m not sure that''s a valid route, but it would look
something like
> > that. In theory, that would minimize the amount of code
you''d need to
> > change in your controllers.
>
> It''s valid route, but it''s a bit "greedy", in
the sense that if you
> also had:
>
> map.connect '':actor_name/:movie_id''
>
> or something, you''d have two
'':wildcard/:wildcard'' routes, of which
> the first to be defined would "win" for all
''x/y'' URLs. This may or
> may not become a problem in a given application, but I''ve found
that
> it does tend to become one :-)
>
> You could tweak it with regex matching for the various wildcards, if
> that''s appropriate.
>
> David
>
> --
> * Books:
> RAILS ROUTING (new!http://www.awprofessional.com/title/0321509242)
> RUBY FOR RAILS (http://www.manning.com/black)
> * Ruby/Rails training
> & consulting: Ruby Power and Light, LLC (http://www.rubypal.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
-~----------~----~----~----~------~----~------~--~---
I m looking for the same... -- 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 -~----------~----~----~----~------~----~------~--~---
Naresh Rana wrote:> I m looking for the same.../domain/action/id/title i want to make my url look likie this,,, /domain/action/title -- 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 -~----------~----~----~----~------~----~------~--~---