Forum: Ruby on Rails Optional parameter in nested routes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Constantin G. (Guest)
on 2008-12-02 03:22
All my charges belong to a company. On the new charge form, sometimes I
specify a company, sometimes I don't because I don't know the company
beforehand in all cases.

So, sometimes the @company object is nil.
I'm using this helper: new_company_charge_path(@company)
For a company object, it generates a path like
/companies//32/charges/new
For a nil object, it generates a path like /companies//charges/new, but
I get an routing error (No route matches...).

I don't want to use an if in my view to use either new_charge_path or
new_company_charge_path. I reckon that /companies//charges/new can be
parseable and rails can recognize the path. Any ideas?

map.resources :charges
map.resources :companies do |company|
    company.resources :charges
end
This topic is locked and can not be replied to.