# Extract controller name from URL

**URL:** <https://www.ruby-forum.com/t/extract-controller-name-from-url/128388>\
**Category:** Rails\
**Created:** [February 7, 2008, 9:30am UTC](https://www.ruby-forum.com/t/extract-controller-name-from-url/128388 "2008-02-07T09:30:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [February 7, 2008, 9:30am UTC](https://www.ruby-forum.com/t/extract-controller-name-from-url/128388/1 "2008-02-07T09:30:46Z")

</div>

Is there a way of extracing controller name from a URL?

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [February 7, 2008, 10:13am UTC](https://www.ruby-forum.com/t/extract-controller-name-from-url/128388/2 "2008-02-07T10:13:15Z")

</div>

I don’t think so because the url could be completely different from  
the controller path because of routes. There’s  
‘controller.controller\_name’ or ‘controller.controller\_path’ (http://  
[ActionController::Base](http://api.rubyonrails.org/classes/ActionController/Base.html#M000433)) which  
you can use to figure which controller’s being used from within the  
app…

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [February 7, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/extract-controller-name-from-url/128388/3 "2008-02-07T12:19:38Z")

</div>

My situation is slightly different. I want to look at  
request.env[‘HTTP\_REFERER’] and take a different action based on what  
is stored in it. For example if the URL belongs to controller a, then  
take one action and if its controller b then take a different action.

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [February 7, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/extract-controller-name-from-url/128388/4 "2008-02-07T13:51:03Z")

</div>

params[:controller] will give you the controller name.  
params[:action] will give you the action name.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
