Only One Controller + Urgent

Hi Champs,

Need Help … :frowning:

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 …

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. [email protected]

Hemant B. wrote:

Hi Champs,

Need Help … :frowning:

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

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.

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 … :frowning:

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

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:

http://guides.rubyonrails.org/routing.html

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’

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs