Forum: Ruby on Rails Retrieving :id without passing it

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric P. (Guest)
on 2006-04-26 16:38
Ok, I have read most of Agile Web Dev... so I had a question about
beautifying URLs using routes.rb. I'm trying to architect a content
management system in which a user can create template pages. This way,
when a user creates a template page nested within, it displays it as
though it's displaying a folder structure, much like a directory tree
(using acts_as_tree).

I have a database table set-up with a page ID, page name (title), page
content, parent_id, etc. My question -- I'm looking for an easy way such
that the url that the app accesses mirrors a similar directory
structure. So, for example...

Home
^ About Us
^^^ Who we really are (nested within About Us)

I would like to map this to
http://localhost:3000/about-us/who-we-really-are

To help with search engine rankings, and to make an easier url to
remember, and it just looks prettier. This is similar to, say, how
http://rails.techno-weenie.net calls the specific questions on his site.
I can currently map the url correctly, however I have no way of
retrieving the proper value of the id from the url to determine what
page to render.

My routes.rb looks as follows:

map.page ':parent_name/:page_name', :controller => 'admin', :action =>
'render_page', :requirements => { :parent_name => /[\w-]+/, :page_name
=> /[\w-]+/ }, :page_name => nil


Can I pass the ID of the page without actually revealing it within the
URL? Or perhaps I can search the DB using LIKE by breaking up the name?
Wilson B. (Guest)
on 2006-04-26 18:35
(Received via mailing list)
On 4/26/06, Eric P. <removed_email_address@domain.invalid> wrote:
> structure. So, for example...
> http://rails.techno-weenie.net calls the specific questions on his site.
>
> Can I pass the ID of the page without actually revealing it within the
> URL? Or perhaps I can search the DB using LIKE by breaking up the name?
>

I'm not claiming this is a 'best-practice' or anything, but I've
always just put a 'short_name' or 'url_title' (choose your own column
name as needed) attribute into the database.
I create it behind the scenes in a before_save method, and do
validates_uniqueness_of on it.
That way it's safe to use as a unique URL, and you can quickly do
"find_by_short_name" to retrieve the instance you're looking for.

Hopefully others have better ideas, because it's always seemed a
little hackish to me, despite being very little code.

--Wilson.
Eric P. (Guest)
on 2006-04-26 21:22
Wilson B. wrote:
> On 4/26/06, Eric P. <removed_email_address@domain.invalid> wrote:
>> structure. So, for example...
>> http://rails.techno-weenie.net calls the specific questions on his site.
>>
>> Can I pass the ID of the page without actually revealing it within the
>> URL? Or perhaps I can search the DB using LIKE by breaking up the name?
>>
>
> I'm not claiming this is a 'best-practice' or anything, but I've
> always just put a 'short_name' or 'url_title' (choose your own column
> name as needed) attribute into the database.
> I create it behind the scenes in a before_save method, and do
> validates_uniqueness_of on it.
> That way it's safe to use as a unique URL, and you can quickly do
> "find_by_short_name" to retrieve the instance you're looking for.
>
> Hopefully others have better ideas, because it's always seemed a
> little hackish to me, despite being very little code.
>
> --Wilson.

I like the idea -- it doesn't sound like too bad of a hack, and is a
definite solution to my question. Thanks a lot for your help, I'll post
back if I hear or a better way.

-e
This topic is locked and can not be replied to.