# Two RESTful routes to same controller - best way to implement

**URL:** <https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586>\
**Category:** Rails\
**Created:** [October 29, 2007, 7:46am UTC](https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586 "2007-10-29T07:46:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schneller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Schneller](https://www.ruby-forum.com/u/Schneller)\
**Post date:** [October 29, 2007, 7:46am UTC](https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586/1 "2007-10-29T07:46:28Z")

</div>

I have a controller that needs to render two different layouts  
depending on the current path, but otherwise performs all the same  
actions.

The routes for the controller are:

/bars  
/foos/5/bars

Both need to call the same controller. The rub, however, is that in  
my views I don’t want to use logic everywhere to determine which named  
route to call depending on whether @current\_foo is set (the id of the  
current foo). Right now I have many bars\_url() and  
bar\_url(@current\_bar) routes - these should generate the proper route  
based on whether the current path is /foos/:id/ or just /.

The only solution I see is to override all the named routes for ‘bar’  
in a helper, and include that helper in my application controller  
(since these routes are used in controllers as well), but that doesn’t  
seem a clean solution.

Is there any way to do this?

---

<div class="post-metadata">

**Author:** ![Schneller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Schneller](https://www.ruby-forum.com/u/Schneller)\
**Post date:** [October 29, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586/2 "2007-10-29T12:23:47Z")

</div>

Maybe resources\_controller  
([http://plugins.ardes.com/doc/resources\_controller/](http://plugins.ardes.com/doc/resources_controller/))  
is what you’re looking for?

---

<div class="post-metadata">

**Author:** ![Schneller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Schneller](https://www.ruby-forum.com/u/Schneller)\
**Post date:** [October 29, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586/3 "2007-10-29T16:18:58Z")

</div>

Thanks for the reply, but we already implemented everything that  
does. I’m asking more about the actual routes - having two routes of  
the same name doesn’t work, so is there a way to add some logic to  
determine which route to use based on the value of an instance  
variable?

On Oct 29, 4:23 am, Gerjan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Schneller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Schneller](https://www.ruby-forum.com/u/Schneller)\
**Post date:** [October 29, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/two-restful-routes-to-same-controller-best-way-to-implement/118586/4 "2007-10-29T16:27:11Z")

</div>

As a further example, to accomplish two routes to same the controller,  
you usually have to do a name\_prefix:

so /bars is bars\_url  
/foo/5/bars is foo\_bars\_url

I want to just use bars\_url everywhere - shouldn’t there be a way to  
determine which route to use based on whether a parameter us set in  
the current path?
