# Routing every request to same action

**URL:** <https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259>\
**Category:** Rails\
**Created:** [March 30, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259 "2009-03-30T16:18:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 30, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/1 "2009-03-30T16:18:02Z")

</div>

Hi,

I have some problem, I want to route every request from URL to an action  
in my one controller.  
Like if I enter localhost:3000/rac .Here it will take the ‘rac’ as a  
controller name and then route it to the ‘index’ action of the ‘rac’  
controller. But what I want is that it should redirect towards  
:controller=\>‘companies’ :action=\>‘index’ , doesnt matter that ‘rac’  
controller exisits or not, It should just redirect to my desired  
controller/action.  
I tried this in my routes.rb  
map.connect ‘parts/:number’,:controller=\>‘companies’ ,  
:action=\>‘index’  
But its not working.  
Please help me to overcome this problem,  
Thanks in advance,  
Regards,  
Umair

---

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 30, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/2 "2009-03-30T23:08:47Z")

</div>

Please let me know if any one know how to do this

---

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 30, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/3 "2009-03-30T23:27:59Z")

</div>

On Mon, 2009-03-30 at 23:08 +0200, Umair Ejaz wrote:

> Please let me know if any one know how to do this

I’m not sure I understand exactly what you want to do but if you really  
want _every_ request to get routed to the same action, you can use a  
before\_filter.

In application.rb:

def route\_all\_to\_somewhere  
redirect\_to :controller =\> ‘desired\_controller’, :action =\>  
‘desired\_action’  
end

and either at the beginning of application.rb or in each of your  
controller files:

before\_filter :route\_all\_to\_somewhere

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 31, 2009, 8:19am UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/4 "2009-03-31T08:19:04Z")

</div>

Thanks dear this will work fine if I write before\_filter  
:route\_all\_to\_somewhere  
in start of my all controllers but if the controller doesnt exist, means  
he write locahost:3000/rac and there is not ‘rac’ controller then how  
can I redirect this request towards my desired action…It willl simply  
give the routing error that “No route matches “/rac” with”. What I want  
is that it should nt gimme this error even if i write the name of the  
controller in URL which doesnt exists, It should always redirect to my  
desiredt action

---

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 31, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/5 "2009-03-31T14:02:34Z")

</div>

If the requirement is to direct every incoming request for a particular  
action in a controller then can just have

map.connect ‘\*path’, :controller=\>“your\_controller”,  
:action=\>“your\_action”

in the routes.rb file. This will redirect every request to ‘your\_action’  
in ‘your\_controller’.

Regards,  
Madhusudhan

---

<div class="post-metadata">

**Author:** ![umair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e495f1/32.png) [@umair](https://www.ruby-forum.com/u/umair)\
**Post date:** [March 31, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/routing-every-request-to-same-action/164259/6 "2009-03-31T20:19:05Z")

</div>

Thank you soooo mcuh!!!thats what i was looking for!  
thanks again
