# Optional parameter in nested routes

**URL:** <https://www.ruby-forum.com/t/optional-parameter-in-nested-routes/154597>\
**Category:** Rails\
**Created:** [December 2, 2008, 2:22am UTC](https://www.ruby-forum.com/t/optional-parameter-in-nested-routes/154597 "2008-12-02T02:22:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [December 2, 2008, 2:22am UTC](https://www.ruby-forum.com/t/optional-parameter-in-nested-routes/154597/1 "2008-12-02T02:22:52Z")

</div>

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
