Skip to content

Instantly share code, notes, and snippets.

@mhriess
mhriess / friendly_urls.markdown
Created November 13, 2012 19:17 — forked from agnellvj/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

Using jQuery

jQuery has become the most popular JavaScript library both inside and outside the Rails community. Let's look at how to take advantage of the library with our applications.

Setup

The setup process differs depending on whether your app is running on a version of Rails before 3.1 or greater than 3.1.

Before Rails 3.1: jquery-rails

@mhriess
mhriess / simple_recursion_examples.scala
Last active August 29, 2015 14:05
simple recursion examples
def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else a + sumInts(a + 1, b)
def countChange(money: Int, couns: List[Int]): Int = {
if (money == 0) 1
else if (money < 0) 0
else if (coins.isEmpty && money >= 1) 0
else {
countChange(money, coins.tail) + countChange(money - coins.head, coins)
}