Conditional root in routes.rb


#1

I am trying to setup role based routing in my application. Something
similar to the following:

map.root :controller => (authorize_admin? ? ‘prospects’ : ‘welcome’)

I am getting an error that authorize_admin? is not defined. It is
defined under lib directory and I can successfully call it from other
places in the application. How do you set up conditional root routes
for an application?

Thanks.

Bharat


#2

On Wed, Apr 1, 2009 at 1:24 PM, Bharat R.
removed_email_address@domain.invalid wrote:

I am trying to setup role based routing in my application. Something
similar to the following:

map.root :controller => (authorize_admin? ? ‘prospects’ : ‘welcome’)

I am getting an error that authorize_admin? is not defined.

Require the file from your lib dir, something like:

require “#{ RAILS_ROOT }/lib/foo.rb”

It is
defined under lib directory and I can successfully call it from other
places in the application. How do you set up conditional root routes
for an application?


Greg D.
http://destiney.com/


#3

Thank you for your response Greg. I found that I was making my code
progressively worse by trying to do the conditional routing in routes.rb
file. Instead, I decided to do it the old-fashioned way as shown below:

    <div id="main-navigation">
      <% if authorize_admin? %>
        <%= navigation [:prospects,{:admin => {:controller => 

‘admin’, :action => ‘index’}}] %>
<% else %>
<%= navigation [{:welcome => root_path}] %>
<% end %>


This is a snippet of code from my application.html.erb file. I will
most likely refactor it later to create the navigation array in the
application_helper.rb once things settle down.

By the way, I tried what you suggest above. I required the file as you
had outlined and then included it since it is a module but that resulted
in a chain of errors since it requires a slew of other things to be
loaded as well.

Regards,

Bharat