Routing URL?

Hello,

I’m trying to do develop some modules so I can reuse all my codes in
different projects.

I need to put routing in different files, and in route.rb include the
files which have my routing code, but I don’t know if this would work,
and if I could actually do it? any help would be good :slight_smile:

Example, this one should be for the account model, are in
route_account.rb.

map.login ‘login’, :controller => ‘accounts’, :action => ‘login’
map.login ‘signup’, :controller => ‘accounts’, :action => ‘signup’
map.logout ‘logout’, :controller => ‘accounts’, :action => ‘logout’
map.account ‘account’ :controller => ‘accounts’, :action => ‘account’

How can I include the following line in route.rb?

Regards,
Jamal

Thanks for your help!

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

Module RouteAccount to class RouteAccount is not working either :frowning:

Russell N. wrote:

Wrap those route definitions in a method and voila!

lib/routes_account.rb…

module RouteAccount
end

RSL

Weird, its not working, I pasted this in the route.rb

RouteAccount.add_routes_to(map) rescue nil

and then I created a file “route_account”, and insert this into it

Module RouteAccount

def add_routes_to(map)

map.login 'login',
  :controller => 'user',
  :action => 'login'

map.login 'signup',
  :controller => 'user',
  :action => 'signup'

map.logout 'logout',
  :controller => 'user',
  :action => 'logout'

map.account 'account',
  :controller => 'user',
  :action => 'account'

end

end

What error is it giving you? Is it saying it doesn’t have a constant
RouteAccount?

RSL

Jamal S. wrote:

Module RouteAccount to class RouteAccount is not working either :frowning:

undefine method when using both module and class…I think I should
“require” somewhere the file?

Jamal S. wrote:

Jamal S. wrote:

Module RouteAccount to class RouteAccount is not working either :frowning:

undefine method when using both module and class…I think I should
“require” somewhere the file?

I tried to write

require ‘lib/route_account’

in the top route.rb that didnt work either…

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. :slight_smile:
Chalk
all my forgetful little errors this morning to lack of coffee. Or
something.

RSL

I just caught that class/module error too. What error are you getting
though?

RSL

Russell N. wrote:

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. :slight_smile:
Chalk
all my forgetful little errors this morning to lack of coffee. Or
something.

RSL

module RouteAccount

class RouteAccount

def add_routes_to(map)

  map.login 'login',
    :controller => 'user',
    :action => 'login'

  map.signup 'signup',
    :controller => 'user',
    :action => 'signup'

  map.logout 'logout',
    :controller => 'user',
    :action => 'logout'

  map.account 'account',
    :controller => 'user',
    :action => 'account'
end

end

end

like this?

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

Russell N. wrote:

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 .

Not working either, I don’t think it will work, I tried everything?

Russell N. wrote:

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

Well I’m sorry, I still don’t get it to work :frowning:

Routing Error
no route found to match “/login” with {:method=>:get}

Can you sent me your files, so I can just replace them with what I have?

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.

  1. Create the file in lib/route_account.rb
  2. 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

  1. 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

  1. 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