Topic: Test custom-made routes on nested resources

Hey

I have this i min route file

map.resources :domains do |domains|  
domains.resources :maildomains, :collection => { :show => :get }
end

To genereate a url like this: domains/16/maildomains;show
# Domain.rb
has_one :maildomain, :dependent => true

# Maildomain.rb
belongs_to :domain


Because the id of the maildomain now should be required to list a maildomain, because its always are connected to a domain in a one-to-one relation, i want to use

show_maildomain_path(:domain_id => 42)

But I cant get it to work. When I access the url in the path, it works, but when using tests, I can't get it to work.
  def test_should_route_to_show_maildomain_of_domain_without_id
    options = {:controller => "maildomains", :action => "show", :domain_id => "1" }
    assert_routing 'domains/1/maildomains;show', options
    assert_equal 'domains/1/maildomains;show', show_maildomains_path(1)
  end

but are getting these errors:

  1) Error:
test_should_route_to_show_maildomain_of_domain_without_id(MaildomainsControllerTest):
ActionController::RoutingError: No route matches {:domain_id=>"1", :controller=>"maildomains", :action=>"show"}
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1266:in `generate'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1214:in `generate_extras'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/routing_assertions.rb:58:in `assert_generates'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions.rb:60:in `clean_backtrace'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/routing_assertions.rb:53:in `assert_generates'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/routing_assertions.rb:81:in `assert_routing'
    test/functional/maildomains_controller_test.rb:55:in `test_should_route_to_show_maildomain_of_domain_without_id'

What can I do to test and use the shortcut to this url?

Re: Test custom-made routes on nested resources

I don't recommend using nested resources here. Instead just make two separate resources:

map.resources :domains, :maildomains

Now you access the maildomain directly, not through a domain:

maildomain_path(1) # => /maildomains/1

Railscasts - Free Ruby on Rails Screencasts

Re: Test custom-made routes on nested resources

Okay, maybe my problem are placed in the wrong thread. But then I have a one-to-one relation, can I then in some way exclude the :id in maildomain, and only use the :domain_id?

Re: Test custom-made routes on nested resources

If you want to use the domain_id, that's an option:

# MaildomainsController
def show
  @maildomain = Domain.find(params[:id]).maildomain
end

But I don't see a good reason to do this. It's best to keep the id passed to the controller the same id as the resource that controller is managing.

Railscasts - Free Ruby on Rails Screencasts

Re: Test custom-made routes on nested resources

I see. I solve the problem by make maildomain to its own resource, as you suggest, and then load @domain in the controller from @maildomain.domain.
Thanks for your time.