Topic: Rewriting URL's

Hi Guys,

I want to create nice URL's that use the page name instead of the row id.  Basically instead of this:

instead of this:

The reason I want to do this is because it makes nicer URL's and because my database contains an active column that denotes whether or not the row is selected for display.  To explain this a little better, this is how the db works:
When you update a page, the db will change the active column from 1 to 0 in the existing entry and add the new entry with an active state of 1.  This allows us to maintain all updates for comparison, roll backs and so on.

I assume it will be a change in routes.rb.  This will be the same for all other controllers as well (some will chow the course_code row instead of name and so on though).


Re: Rewriting URL's

Hi teknetia

You could rewrite your URL's like this

This will make your URL's more SEO friendly. Here's an article that goes into more detail

Hope this helps

Re: Rewriting URL's

The problem with this is that when I change the home page, the number will be different, meaning that perma-links won't work, neither will bookmarks, etc.

That's what I want to omit the id entirely.

On a side note, when I validates_uniqueness_of, can i validates_uniqueness_of where active="1"?

Last edited by teknetia (2007-07-16 07:10:11)

Re: Rewriting URL's

hmmm, i see you point. Personally if i had to impliment something like this, i would not allow the page name to be changed. My solution is to keep the page name and the actual page title separate. I'm asuming your DB has a field for page name and content?

I would do something like this:

where "home" is the page name and use a partial that could look something like this:
<h2><%= h(page.title) %></h2>
<div id="content">
  <%= h(page.content) %>

And on your side note i don't think that solution will be a good one as it will conflict with other pages that are active and that must remain active.

I think it is possible to do a custom validation.

Re: Rewriting URL's

The page name cannot be changed, it is only set when the page is first created.  The problem is that it will exist in multiple rows in the database, but only once where the active column = 1.

I want to remove the reference to :id from the URL entirely.  I do not want the URL to have any reference to the id as the id will change with each update.  Also, having id's in url's is just not nice for human readability.

I thought that maybe I would have to do something that defined the way the route worked in routes.rb.

Any more ideas please?

Re: Rewriting URL's

I've had the same problem with my own static web controller and decided to go the id route and having a page like this:

I've also thought the same, but have not been able to find a solution.
In my mind something like this is what you are looking for in your routes.rb
map.connect 'page/:page_name'

Unfortunatly i don't know how to do it