Skip to content

Instantly share code, notes, and snippets.

agnellvj / friendly_urls.markdown
Created September 11, 2011 15:52 — forked from jcasimir/friendly_urls.markdown
Friendly URLs in Rails
View friendly_urls.markdown

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:


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.


1. What is the difference between functionOne() and functionTwo() and what will be the output?

var functionOne = function() { 
  return “Hi!  Hello I am functionOne”;
function functionTwo() {
  return “Hi!  Hello I am functionTwo”;
View Spiralizer.exs
# A B C D
# E F G H
# I J K L
# M N O P
# a b c d h l p o n m i e f g k j"
# top, right, bottom, left
defmodule Spiralizer do
defp top(acc, []) do
View SpiralizerTest.exs
Code.load_file("Spiralizer.exs", __DIR__)
ExUnit.configure exclude: :pending, trace: true
defmodule SpiralizerTest do
use ExUnit.Case
test "empty list" do
data = []
agnellvj /
Created January 28, 2012 00:18 — forked from ddemaree/
How Sunspot implements its wonderful search/index DSL

This code is extracted/adapted from Mat Brown's Sunspot gem. One of Sunspot's nicest features is an expressive DSL for defining search indexes and performing queries. It works by instance_eval-ing a block you pass into it in the context of its own search builder object. In this code, the pig thing1 statement is roughly equivalent to zoo =; zoo.pig(thing1).

Sunspot's DSL has to resort to trickery: the instance_eval_with_context method uses eval to get the block to give up the object it considers to be self, then sets up an elaborate system of delegates and method_missing calls so any methods not handled by the DSL are forwarded to the surrounding object. But as a result, this syntax is minimal and beautiful, and it works the way you expect whether or not you prefer blocks to yield an object.

Without this trick the block would be restricted to either the original, "calling" context (as a closure) or the DSL's "receiving" context (using instance_eval), but not both.

Using `ins

View DigitalColor-Meter-fix.scpt
-- Mountain Lion (10.8) fixed this oversight. The DigitalColor Meter now remembers it's settings on exit.
-- DigitalColor Meter defaults to displaying color values in decimal and will not remember hexidecimal preferences on close. So this script launches the app and tells it to display values in hex. It is meant to be launched via QuickSilver or a launcher.
-- Checks to see if System Preferences > Universal Access > Enable access for assistive devices is checked
-- This option is required for "System Events" to use the keystroke and key code commands.
-- If it is not checked, your password is required to make the change
tell application "System Events" to if not UI elements enabled then
set UI elements enabled to true
agnellvj / gist:2663505
Created May 12, 2012 01:08 — forked from avalanche123/gist:981817
GitHub Emoji
View gist:2663505
@Test public void testSimpleScroll3() throws Exception {
try {
} catch (Exception e) {
// ignore
client.admin().indices().prepareCreate("test1").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2))
View jquery_in_rails.markdown

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.


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