Optional parameter in nested routes


#1

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