views:

42

answers:

1

Hi,

I'm using Rails 3.0 and I have the following in my routes.rb files:

scope "/:location" do
  resources :agents
end

So I can do this:

agents = Agent.where("location = ?", params[:location])

(There might be a better way to do this..? Mostly I want URLs like: myurl.com/london/agents)

Anyway, my problem is with the tests (which I'm still learning), how can I make this work with the scope:

class AgentsControllerTest < ActionController::TestCase
  test "should get index" do
    get 'index'
    assert_response :success
  end
end

It just gets a route no found error.

Any help would be great, thanks.

+1  A: 

Because you have scoped your resources, you don't actually have direct access to the agents_controller via any actions. So calling get 'index' is really just trying to access /agents. If you tried this in your browser, it would fail as well.

To get a success you'll have to supply a location param like so:

get 'index', { :location => 'hawaii' }

Hope that helps.

Edit: If you want additional access to the agents_controller (meaning location is optional) simply add a top-level resource for it in your routes file:

resources :agents
jenjenut233
That's great, thanks! (I actually thought I'd tried that and it failed, but I must have done something wrong as it all passes now - so cheers)
Delameko