Skip to content

Instantly share code, notes, and snippets.

@JeanMertz
Created April 12, 2011 13:16
Show Gist options
  • Save JeanMertz/915480 to your computer and use it in GitHub Desktop.
Save JeanMertz/915480 to your computer and use it in GitHub Desktop.
# spec/controllers/users_controller_spec.rb
# ERROR:
# Failure/Error: get :edit, :id => user.id
# ActionController::RoutingError:
# No route matches {:id=>1, :controller=>"users", :action=>"edit"}
describe UsersController do
render_views
it "renders edit template for edit action" do
user = Factory(:user)
get :edit, :id => user.id
response.should render_template(:edit)
end
...
end
# spec/controllers/users_controller_spec.rb
# ERROR:
# Failure/Error: get :edit, :id => user.id, :account_id => user.account_id
# NoMethodError:
# undefined method `users' for nil:NilClass
describe UsersController do
render_views
it "renders edit template for edit action" do
user = Factory(:user)
get :edit, :id => user.id, :account_id => user.account_id
response.should render_template(:edit)
end
...
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :current_account
helper_method :current_account
private
# Set current domain to get account data
def current_domain
url = Domainatrix.parse(request.url)
url.domain + "." + url.public_suffix
end
# Set current_account to compare against database
def current_account
@current_account ||= Account.find_by_domain(current_domain)
end
end
# app/controller/users_controller.rb
class UsersController < ApplicationController
def edit
@user = current_account.users.find(params[:id])
end
...
end
# app/model/account.rb
class Account < ActiveRecord::Base
has_many :users
...
end
# app/model/user.rb
class User < ActiveRecord::Base
belongs_to :account
...
end
# spec/factories.rb
Factory.define :account do |f|
f.domain "smackaho.st"
end
Factory.define :user do |f|
f.email "foo@bar.com"
f.fname "Foo"
f.name "Bar"
f.password "test123"
f.password_confirmation { |u| u.password }
f.association :account
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment