# Only One Controller + Urgent

**URL:** <https://www.ruby-forum.com/t/only-one-controller-urgent/171807>\
**Category:** Rails\
**Created:** [July 9, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807 "2009-07-09T15:14:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/1 "2009-07-09T15:14:57Z")

</div>

Hi Champs,

Need Help … ☹

Actually the thing is that i am having many controllers a, b, c and d…  
But what i want it that only “a” should be displayed to the users … b,  
c, d not …  
Is there any way to do it … ?

Cheers …

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/2 "2009-07-09T15:23:08Z")

</div>

Hello,

I think you can add a before\_filter in your application\_controller so  
that  
it prevents the display when params[:controller] is not a.  
Hope it helps !

Cyril.

2009/7/9 Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/3 "2009-07-09T15:25:36Z")

</div>

Hemant B. wrote:

> Hi Champs,
> 
> Need Help … ☹
> 
> Actually the thing is that i am having many controllers a, b, c and d…  
> But what i want it that only “a” should be displayed to the users … b,  
> c, d not …  
> Is there any way to do it … ?
> 
> Cheers …

Yes, you can do this a couple of ways…

The first way is you can inhibit the routing of controllers you don’t  
want people to access by doing something similar:

Remove the routing to that controller completely OR  
map.resources :controller\_name, :only =\> :method\_name

You could setup a catchall method using the above and have it redirect  
to some other controller etc…

OR

Place the following at the top of your controllers:

class YourController \< ApplicationController  
before\_filter :login\_required, :authorize

…  
end

… which will force them to login and then it will call an authorize  
function to check if they are an admin role…

Place the following in application\_controller.rb

private

def authorize  
unless logged\_in? && User.find(current\_user).admin?  
redirect\_to root\_url  
end  
end

… and create an admin role on your users table

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/4 "2009-07-09T15:27:17Z")

</div>

OR, like was mentioned above

Create any method that checks for what conditions you are specifying to  
access that controller through a before\_filter

My authorize example works great for users but you may or may not have a  
users database setup.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/5 "2009-07-09T15:30:31Z")

</div>

What is the first step you are describing here … ?  
i did not get it properly … ?

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> Hemant B. wrote:
> 
> > Hi Champs,
> > 
> > Need Help … ☹
> > 
> > Actually the thing is that i am having many controllers a, b, c and d…  
> > But what i want it that only “a” should be displayed to the users … b,  
> > c, d not …  
> > Is there any way to do it … ?
> > 
> > Cheers …
> 
> Yes, you can do this a couple of ways…
> 
> The first way is you can inhibit the routing of controllers you don’t  
> want people to access by doing something similar:

In Which controller or config/routes.rb file …

> Remove the routing to that controller completely OR  
> map.resources :controller\_name, :only =\> :method\_name
> 
> You could setup a catchall method using the above and have it redirect  
> to some other controller etc…
> 
> OR

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 9, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/6 "2009-07-09T15:38:50Z")

</div>

First, how are you creating these controllers? Are you creating them  
RESTfully? Are you using generate controller or generate scaffold?

If you are generating via scaffold then inside your routes.rb file  
you’ll see something like:

map.resources :controller

Mapping is what creates the routes for this. Keep in mind that at the  
bottom of that file you may have:

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/action/id.:format’

These two lines basically setup the controller, action, id routing to  
every controller you create. If you don’t want these to manage those  
routes you need to comment them out:

# map.connect ‘:controller/:action/:id’

# map.connect ‘:controller/action/id.:format’

Then, you setup explicit routing using the examples I gave. If you are  
having trouble with routes then you need to research them a bit and get  
familiar with them. They are very important for working with rails:

> **[Rails Routing from the Outside In — Ruby on Rails Guides](https://guides.rubyonrails.org/routing.html)**
>
> Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred...

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 10, 2009, 8:27am UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/7 "2009-07-10T08:27:06Z")

</div>

Thanks a lot to all of you …  
Really Helpful …

Cheers …

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> First, how are you creating these controllers? Are you creating them  
> RESTfully? Are you using generate controller or generate scaffold?
> 
> If you are generating via scaffold then inside your routes.rb file  
> you’ll see something like:
> 
> map.resources :controller
> 
> Mapping is what creates the routes for this. Keep in mind that at the  
> bottom of that file you may have:
> 
> map.connect ‘:controller/:action/:id’  
> map.connect ‘:controller/action/id.:format’
> 
> These two lines basically setup the controller, action, id routing to  
> every controller you create. If you don’t want these to manage those  
> routes you need to comment them out:
> 
> # map.connect ‘:controller/:action/:id’
> 
> # map.connect ‘:controller/action/id.:format’

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 21, 2009, 9:25am UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/8 "2009-07-21T09:25:59Z")

</div>

Can i do this thing in config/routes.rb …?  
If yes … How can i … ?

Hemant B. wrote:

> Thanks a lot to all of you …  
> Really Helpful …
> 
> Cheers …
> 
> Ã„lphÃ¤ BlÃ¼Ã« wrote:
> 
> > First, how are you creating these controllers? Are you creating them  
> > RESTfully? Are you using generate controller or generate scaffold?
> > 
> > If you are generating via scaffold then inside your routes.rb file  
> > you’ll see something like:
> > 
> > map.resources :controller

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 21, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/only-one-controller-urgent/171807/9 "2009-07-21T15:30:30Z")

</div>

quite interesting problem  
I think best way to add :before\_filter to your controllers

routing will restrict even the admin level user to access the  
controllers.

if you want to restrict all users including the admin then you have to  
think how  
can you access that controller from the internal system (because if  
you are  
using restful routing and have not define anything for that controller  
then you will not be able to  
access those controller. in that case you have to rethink about the  
design i mean whether you really need those controllers)

if this one is the authorization problem then :before\_filter is the  
best solution
