Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Creating nice url's
9 Posts
Creating nice url's


I've got a question about creating good urls. I'm creating a site in 2 languages (dutch and french) and I want to create permalinks on the fly. This is how I do it now (this is a method in my model)

[code] def dutch_url



Is this a good way to do this? Or how could I do this better?

Thanks for your help

the best way I think, apply the locale id as part of your URL, looks like "/en/articles/1", that's will make page caching quite easy without any additional codes.

Step 1 - configure your routes setting

map.with_options(:prefix => ':locale') do |i18n|
  i18n.resources :article

Step 2 - append a before filter to your ApplicationController

def set_locale
  I18n.locale = params[:locale]

Magic done. To get more details or examples, follow "this link":http://railscasts.com/episodes/138-i18n for a screencast on Railscasts.com

Thanks for your advice : one more question how can I create url looking like this 1-title-dutch for example? Second question : in the railscast I see that it's possible to use several languages, but I've build my db so that the dutch and the french version are in the database. The navigation and content is set dynamicaly, but how can I do this using I18N?


Your first question is quite easy. While calling URL generator for a model class, it is actually calling it's to_params method.

  class Article < ActiveRecord::Base
    def to_params
      return "#{id}-#{title.parameterize}"

this will returns a pretty id for url.

And the second one, I haven't find out a perfect way to deal with. Maybe other rails guys could help.

Thanks for the to_param, that gives me 1 last question. You know that I have 2 languages that commes with 2 kinds of navigations (a dutch one and a french one).

How can I implement this with 2 languages? Do I have to make 2 defs like I did above with the dutch_url?

Thanks for your help

However, I haven't totally understand the second question about the "navigation" ;-p

Usually this comes in two conditions:

for static navigation: the navigation contents are static - do not relate with database, just using Rails I18n and compose the translations under "/config/locale"

for dynamic navigation: contents are dynamic - saved in database and will be loaded while requesting this page, pre-execute a query for loading contents and then render a navigation template

# e.g. Init the navigation in sidebar if necessary
# controller
before_filter :load_navigation
def load_navigation
  @navigation_items = NavigationItem.find_all_by_locale(params[:locale])

# sidebar template

I'll try to explain my situation a bit more clear.

The site I'm making is a multi language site a Dutch part and a French part. When you enter the site, there will be a splash page where you can choose your own language. (now I have 2 controllers one for each language)

And I'm finding a way to do this.

The problem I'm facing is that the function below only creates an url for 1 language only. (for example dutch)

def to_params

  return "#{id}-#{titleDutch.parameterize}" 


How can I fix this?

Now I got it.

For example, in this url "193-this-is-an-example", only "193" is significative and "this-is-an-example" is useless for Rails application (however it is still useful as a part of friendly url). One target (such as a post, a book, etc) in different language will be saved as different records in database, with different id

# Example controller, maybe ArticlesController
def show
  if params[:id] =~ /^(\d+)\-.+$/
    @article = Article.find($1)
    redirect_to articles_path

There is no need to fix the rest part of URL because they are useless while finding an record, just ignore them and your user will not pay attention on the difference between "192-say-hello" and "193-你好". The most important thing is, it works well as we expected, both these two url point to the one article with greeting words in different language. I think that should be what you actually need.

hi Jerry,

We're almost there:)! Just 1 more thing, how can you generate 2 urls within 1 model?

For example : "1-im-a-dutch-url" -> is generated by the to param method. But how can I generate 1-im-a-french-url.

I'll also post my table fields so that it's completely clear: id:int page_category_id:int title_dutch:string(255) title_french:string(255) content_dutch:text content_french:text

The controller part works fine:)

Edit: 1 row has both the dutch and french content.

Thanks for your help

9 Posts
Login to add your message