Wrap those route definitions in a method and voila!
lib/routes_account.rb…
module RouteAccount
def add_routes_to(map)
map.login ‘login’, :controller => ‘accounts’, :action => ‘login’
# I presume the next one should actually be map.signup, not map.login...
map.login 'signup', :controller => 'accounts', :action => 'signup'
map.logout 'logout', :controller => 'accounts', :action => 'logout'
map.account 'account' :controller => 'accounts', :action => 'account'
end
end
config/routes.rb…
ActionController::Routing::Routes.draw do |map|
I found that I had problems loading the routes if I didn’t load them first. YMMV
RouteAccount.add_routes_to map rescue nil
Continue normal routing…
end
RSL
Obviously, that should be a class method! Oops. That’s what I get for typing rather than cutting and pasting from my app.
RSL
What error is it giving you? Is it saying it doesn’t have a constant RouteAccount?
RSL
I just caught that class/module error too. What error are you getting though?
RSL
It’s seeing the class, just not the method. Perhaps you missed my email about making the method a class method instead of a instance one. Chalk all my forgetful little errors this morning to lack of coffee. Or something.
RSL
Sorry I wasn’t clearer.
class RouteAccount
def self.add_routes_to(map)
map.login ‘login’, :controller => ‘accounts’, :action => ‘login’
# I presume the next one should actually be map.signup, not map.login...
map.login 'signup', :controller => 'accounts', :action => 'signup'
map.logout
‘logout’, :controller => ‘accounts’, :action => ‘logout’
map.account 'account' :controller => 'accounts', :action => 'account'
end
end
Like that.
Not only does it work but I’ve got two variations of it working in my app. If you’d like, I can help you more but not if you’re insistent on telling me that it doesn’t work.
RSL
No, I cannot just send you my files. Heh. However. I’m more than willing to go over [in one email] what I’ve suggested you do.
- Create the file in lib/route_account.rb
- Create the class [not module as I mistakenly first said] RouteAccount and a class method add_routes_to…
class RouteAccount
Make sure this is self.add_routes_to(map)
def self.add_routes_to(map)
map.login ‘login’, :controller => ‘accounts’, :action => ‘login’
map.signup ‘signup’, :controller => ‘accounts’, :action => ‘signup’
map.logout ‘logout’, :controller => ‘accounts’, :action => ‘logout’
map.account
‘account’ :controller => ‘accounts’, :action => ‘account’
end
end
- In your config/routes.rb, call this method before you set the normal routes…
ActionController::Routing::
Routes.draw do |map|
RouteAccount.add_routes_to map # Just like that.
Then add your other routes here
end
- At the end of the routes.rb file [AFTER you close the routes.draw block] you can add the line:
ActionController::Routing::Routes.routes.each{|r| puts r}
which will output a listing of all the routes to the terminal you started your server from. You’ll want to comment that out in production mode of course.
All this code does is take the route map that the Routes.draw creates/uses and sends it to a class method which adds routes to it. Nothing weird. Just a different place than normally routes are set. I got the idea from looking around Mephisto’s source. Rick is so far above me as a programmer that my implementation is a lot simpler but basically the same.
RSL