Forum: Ruby on Rails conditional root in routes.rb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D3fc5887a2f39f2e0c8989d39ce5e6f9?d=identicon&s=25 Bharat Ruparel (bruparel)
on 2009-04-01 20:24
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
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-04-01 23:28
(Received via mailing list)
On Wed, Apr 1, 2009 at 1:24 PM, Bharat Ruparel
<rails-mailing-list@andreas-s.net> 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 Donald
http://destiney.com/
D3fc5887a2f39f2e0c8989d39ce5e6f9?d=identicon&s=25 Bharat Ruparel (bruparel)
on 2009-04-02 14:09
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 %>
          <div class="clear"></div>
        </div>

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
This topic is locked and can not be replied to.