Skip to content

Instantly share code, notes, and snippets.

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.

@iantropov
iantropov / issue_5588.rb
Created November 10, 2013 06:31
Test case for Rails issue #5588
gemfile_path = "#{File.basename(__FILE__, '.rb')}.gemfile"
ENV['BUNDLE_GEMFILE'] = File.expand_path(gemfile_path)
unless File.exists?(gemfile_path)
File.write(gemfile_path, <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.0.0'
gem 'sqlite3'
GEMFILE
@iantropov
iantropov / issue_7807.rb
Last active December 27, 2015 21:49
Test case for Rails issue #7807
gemfile_path = "#{File.basename(__FILE__, '.rb')}.gemfile"
ENV['BUNDLE_GEMFILE'] = File.expand_path(gemfile_path)
unless File.exists?(gemfile_path)
File.write(gemfile_path, <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.0.0'
gem 'sqlite3'
GEMFILE
@iantropov
iantropov / gist:6743621
Created September 28, 2013 16:12
Reproducing without additional gems and with sqlite
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.0'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
@iantropov
iantropov / gist:6742197
Created September 28, 2013 13:43
Foreign key violation, caused by incorrect order of destroying linked objects (link is 'belongs_to')
gem 'activerecord', '4.0.0'
require 'active_record'
require 'minitest/autorun'
require 'logger'
gem "schema_plus"
require 'schema_plus'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(
@iantropov
iantropov / gist:5516329
Created May 4, 2013 05:29
Postgis 2.0 on Win8 64
If you have a libxml2.dll, try copying over to libxml2-2.dll and see if that solves your problem.
@iantropov
iantropov / gist:5502118
Created May 2, 2013 13:21
Rmagick on windows
1. So edit the gems/rmagick-2.13.1/ext/RMagick/extconf.rb file at #171 and #178 like below: "`identify -version` =~ /Version: ImageMagick (\d+\.\d+\.\d+)-\d+ /"
2.1) Ruby => http://rubyforge.org/frs/download.php/74298/rubyinstaller-1.9.2-p180.exe
2) DevKit => http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe
3) ImageMagick => http://www.imagemagick.org/download/binaries/ImageMagick-6.6.7-9-Q16-windows-dll.exe !!!!!32bits
4) RMagick => https://github.com/rmagick/rmagick/zipball/master
5) type-windows.xml => http://www.imagemagick.org/source/type-windows.xml
3. 1) Ruby => c:\Ruby192
2) DevKit => c:\DevKit
@iantropov
iantropov / gist:4986762
Created February 19, 2013 15:15
Jquery event handlers
$._data( elem ).events
@iantropov
iantropov / Rails debbugger instalation
Created January 22, 2013 15:17
After these lines I got possibility to debug in Aptana. But "rails server --debugger" wan`t start. Ruby 1.9.3. Rails 3.11.
gem install ruby-debug-ide --pre
gem install ruby-debug-base19x --pre
@iantropov
iantropov / reps.py
Last active December 10, 2015 23:38
First implementation of parser for Yandex`s result pages
from lxml import html
class YandexRespParser(object):
__RESULTS_ON_PAGE = 10 #Its possible to evaluate this number from first result page
def __create_rep(self, result_element):
rep = {}
rep["index"] = int(result_element.xpath('.//b[@class="b-serp-item__number"]')[0].text_content())