Skip to content

Instantly share code, notes, and snippets.

@Anexo
Last active August 29, 2015 14:06
Show Gist options
  • Save Anexo/b09d3207489280c41789 to your computer and use it in GitHub Desktop.
Save Anexo/b09d3207489280c41789 to your computer and use it in GitHub Desktop.
Friendly URLs in Rails with Latin or Special Charachters

Friendly URLs in Rails with Latin or Special Characters:

Based on the Gist by: jcasimir -> https://gist.github.com/jcasimir/1209730#friendly-urls !!

By default, Rails app's build URL based on the primary key (the id) column of the database.

Imagine we have a User model and associated controller. Our first User is called John Doe and has an id = 1. The Url of his user page would be:

/user/1

As we can see, the user's slug (last segment of the URL) is 1. For aesthetic purposes and more important, for SEO purposes, we have no information about the user.

As I mentioned before, jcasimir gave us a few ways on how to improve this slug:

(From jcasimir Gists, with slight modifications):

Simple Approach & Slug Generation

  The simplest approach is to override the to_param method in the User model. [...] We can override to_param to include a parameterized version        of the user's name:

    class User < ActiveRecord::Base
      def to_param
        [id, name.parameterize].join("-")
      end
    end

For our user John Doe with id number 1, this will generate a slug 1-john-doe. The full URL would be:

  /user/1-john-doe

The parameterize method from ActiveSupport will deal with converting any characters that aren't valid for a URL.

Problem with Latin or other Special characters:

If we follow this approach for a User called Tomás López with id = 2, we see it's got two characters that after being parameterize, his URL will look like this:

/user/2-tom-s-l-pez

As you see, it's not useful either in a SEO or aesthetical way.

We will we using the gsub method of Ruby on Rails to get the problem solved.

First of all we create the to_param method in our model (in this case in our User model):

def to_param
    "#{id}-#{slug}"
end

As you can see, we've changed the code slightly, defining the to_param method to call a method called slug.

This other method looks like this:

def slug
    url = name.downcase 
    url.gsub!(" ", "-") 
    url.gsub!("á", "a") 
    url.gsub!("é", "e")
    url.gsub!("í", "i")
    url.gsub!("ó", "o")
    url.gsub!("ú", "u")
    url.gsub!("ñ", "n")
    url = url.parameterize
    return url
end

First we downcase all the letters in the user's name. Using the gsub method, we can change the special characters we want. Finally we parameterize the string in order to solve any problem or character we could have forgotten.

Our final code in the User model would be:

def slug
    url = name.downcase 
    url.gsub!(" ", "-") 
    url.gsub!("á", "a") 
    url.gsub!("é", "e")
    url.gsub!("í", "i")
    url.gsub!("ó", "o")
    url.gsub!("ú", "u")
    url.gsub!("ñ", "n")
    url = url.parameterize
    return url
end 

def to_param
    "#{id}-#{slug}"
end

Resulting in the following URL for our user Tomás López:

/user/2-tomas-lopez

Benefits & Limitations:

As you see, with this method you can use any special character you need.

IMPORTANT: Do not forget to include the encoding your model will use at the top of the model code, for example: #encoding utf-8

We've added content to the slug which will improve SEO and make URLs more readable, but we cannot manipulate the URL in any meaningful way.

For this please refer to jcasimir Gist -> https://gist.github.com/jcasimir/1209730#simple-approach

¡Hasta luego!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment