Installing gem Stringex for Rails? Don't forget to migrate
I was setting up Stringex for my existing User model following the Readme (there aren't actually any installation instructions) and guessed I needed to add the gem to my Gemfile:
# Gemfile
gem "stringex"
And set up my models and controllers:
# user.rb
acts_as_url :name, :sync_url => true
def to_param
url
end
# users_controller.rb
load_and_authorize_resource :find_by => :url
I am using CanCan here.
So it struck me, when I then received routing errors:
ActionController::RoutingError at /users
No route matches {:action=>"show", :controller=>"users", :id=>#<User id: 1, email: ...
Turns out, you need to add some columns first:
$ rails g migration AddUrlToUsers url:string:uniq
# 20130404102715_add_url_to_users.rb
class AddUrlToUsers < ActiveRecord::Migration
def up
add_column :users, :url, :string, :nil => false
add_index :users, :url, :unique => true
# You'll want to generate the URLs for all existing users:
User.initialize_urls
end
def down
remove_index :users, :url
remove_column :users, :url
end
end
But that's obvious, isn't it?
Follow-up: Redirecting old ID routes to new slugs
Written by Robert Wünsch
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#