In my app, I've got a Page model and in my routes.rb have the line:
map.resources :pages
This gives me all of the nice urls for all of the CRUD actions.
However, for showing pages, I didn't like the urls pages/5 wasn't very
descriptive. I preferered 'nice' urls such as blog/about-me. I could
use the to_params method to specify using permalinks instead, but you
still had to put pages/ in front of the permalink and I couldn't find
a way to avoid having the model name at the start of urls.
I've come up with a route that solves this problem and people can
access the pages using this 'nice' notation and I can generate it
using the helper nice_page_path(@page).
But since I don't actually use the page_path(@page) helper it would be
nice if I could use this to generate my 'nice' paths instead of having
a custom helper called nice_page_path. Is there an option you can pass
to map.resources to say that you don't want one of the routes? Or is
there a way of overriding this helper so I can use it for my paths
instead?
But since I don't actually use the page_path(@page) helper it would be
nice if I could use this to generate my 'nice' paths instead of having
a custom helper called nice_page_path. Is there an option you can pass
to map.resources to say that you don't want one of the routes? Or is
there a way of overriding this helper so I can use it for my paths
instead?
You might think it would be nice, but it sort of defeats the one of the
purposes of REST, which is for all sites to follow a standard for the
basic 7 actions. If you have a route that works the way you want it to,
and a helper that produces the url's the way you want it to, why do you
feel you need to wreck/disable the standard RESTful actions and the rest
of the world's RESTful access to your site content? As long as they can
get your content with "pretty" url's when they want to, is there really
a problem with people/programs still being able to reach your content
via "/pages/5"?
That's a good point Cayce, thanks. I was thinking of keeping both ways
of accessing the pages and now you've helped to convince me that I
should keep that convention in place.
I still think that using permalinks such as /blog/about-me is much
more descriptive than pages/5 and also think that in this one case a
'pages' model is one where you wouldn't want the url to be preceded by
the word 'pages'. I can certainly understand why you would in other
cases - 'products', 'episodes' etc, but it is usually taken as a given
that what you are looking at is a page (although maybe not when we are
supposed to be assuming that everything is a resource....)
Thanks for clearing that up Sheldon. Like I said already, having names
like /blog/about-me makes more sense in this case because it also give
information about the parent page (blog) of the page being found
(about-me), so adds some scope to the url that you wouldn't get from /
pages/5.
I'm in two minds here - should I allow both urls as some people might
expect the 'convention' of /pages/5 or should I try to keep things
like the id of the page private to the application?
Also, with regard to my original question, is it possible to EXCLUDE
some of the helpers from map.resources? Should it be possible? If not
and this isn't the purpose of REST then why force this style of url?
I'm in two minds here - should I allow both urls as some people might
expect the 'convention' of /pages/5 or should I try to keep things
like the id of the page private to the application?
That's hard for me to answer, because I can't imagine a situation in
which an outside consumer of your web service could possibly know that
it's interested in doing something with page 5, without already knowing
enough about page 5 to know that its slug is about-me.
That said, the existence of the unpublished routes isn't hurting you.
Also, with regard to my original question, is it possible to EXCLUDE
some of the helpers from map.resources?
No. The whole point of ActionController::Resources#resources is to map
routes to a "RESTful resource". That has specific meaning, and
includes things like: if you GET /page/5, I'll route you to the
PageController's show action, passing it an id parameter of 5.
Perhaps the confusion here comes from people collapsing the term REST
with the term RESTful resource.
If you don't want to implement RESTful resources, don't use
ActionController::Resources#resources to map routes.
But again, they're not hurting you.
Personally, I don't mind pretty URLs when someone else implements them
in software I use (e.g. Mephisto Blog), but I never implement them
myself. I think they're pointless, particularly in the kinds of
applications I write.