Forum: Ruby on Rails Routing help.... it is so difficult!

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.
Bao L. (Guest)
on 2006-06-08 12:57
hi guys, sorry to trouble you all but i really dont understand how this
routing thing work... it seems easy but it just dont work for me!

here's the situation :

my url b4   : http://127.0.0.1:3001/admin/login
my url after: http://127.0.0.1:3001/burninglegion/admin/login

i wanna do something like this so i went to the routes.rb and type this:

ActionController::Routing::Routes.draw do |map|

  map.connect "burninglegion/:controller/:action/:id" #i typed this
  map.connect ':controller/service.wsdl', :action => 'wsdl'
  #default
  map.connect ':controller/:action/:id'
end

ok when i typed the initial url
http://127.0.0.1:3001/burninglegion/admin/login, it would display the
page but when it logged in, it changed to

http://127.0.0.1:3001/admin/main

which missed the constant name.... is wat i do correct???

some i got like 3 controllers and i hope that it would apply to everyone
of them...is that possible???

thanks thanks
Steven H. (Guest)
on 2006-06-08 20:22
(Received via mailing list)
In your case, you could do the following:

map.connect "burninglegion/admin/login",
             :controller => "some_controller",
             :action => "some_action"



If you want more explanation, take the below url and the defined route.

http://mysite.com:3000/all/eudora/7.0.1/windows/download/5

map.download
':platform_filter/:software/:version/:platform/:distribution_channel/:id',
    :controller => 'user/version',
    :platform_filter => /all/,
    :platform => /windows/linux/,
    :distribution_channel => /download/,
    :action => 'download',
    :id => /\d+/


Here's how--I think--the match is determined:

   1. Rails looks at the first portion of the url--"all"--and checks to
      see if it will be accepted by my route as :platform_filter. It
      matches my regex constraint for :platform_filter, so
      platform_filter => "all" is set.
   2. Rails inspects the next url item--"eudora"--checks to see if it
      matches my mapping constraint. There is no constraint, so it
      matches, sets :software => "eudora".
   3. Rails inspects the next url item--"7.0.1"--checks to see if it
      matches my mapping constraint. There is no constraint, so :version
      => "7.0.1" is set.
   4. Rails inspects the next url item--"windows"--checks to see if it
      matches my mapping constraint. It matches my regex constraint, so
      platform => "windows" is set.
   5. Rails inspects the next url item--"download"--checks to see if it
      matches my mapping constraint. It matches my regex constraint, so
      :distribution_channel => "download" is set.
   6. Rails inspects the last url item--"5"--checks to see if it matches
      my mapping constraint. It matches my regex constraint, so :id =>
      "download" is set.
   7. All the url portions mapped, so this route will be used.
   8. Rails looks at the :controller and the :action defined in this
      route and executes them, passing all the matchig url portions via
      the params hash. Ex: we can access the :software portion of the
      url like this--params[:software]

If any of the above steps do not match, the route is skipped, the keys
are cleared and the process is repeated with the next route.


I hope this helps,
Steven
Alder G. (Guest)
on 2006-06-08 20:38
(Received via mailing list)
On 6/8/06, Steven H. <removed_email_address@domain.invalid> wrote:
>
>
>    3. Rails inspects the next url item--"7.0.1"--checks to see if it
>       "download" is set.
> I hope this helps,
> > here's the situation :
> >   #default
> >
>
Excellent explanation. Kudos, Steve.
Bao L. (Guest)
on 2006-06-09 06:18
Alder G. wrote:
> On 6/8/06, Steven H. <removed_email_address@domain.invalid> wrote:
>>
>>
>>    3. Rails inspects the next url item--"7.0.1"--checks to see if it
>>       "download" is set.
>> I hope this helps,
>> > here's the situation :
>> >   #default
>> >
>>
> Excellent explanation. Kudos, Steve.

so do you mean i have to explicitly define all the routes?
 ok in admin i have the following actions like login, logout, main,
search and add_user so i have to explicitly state all of them? like
this?

map.connect "burninglegion/admin/login",
             :controller => "admin",
             :action => "login"

map.connect "burninglegion/admin/logout",
             :controller => "admin",
             :action => "logout"

map.connect "burninglegion/admin/search",
             :controller => "admin",
             :action => "search"

map.connect "burninglegion/admin/add_user",
             :controller => "admin",
             :action => "add_user"

map.connect "burninglegion/admin/main",
             :controller => "admin",
             :action => "main"

omg.... is there a more dynamic way??? cos i just wanna add my name in
front of all the url only T-T
Ken K. (Guest)
on 2006-06-09 06:32
bao lee wrote:
>
> so do you mean i have to explicitly define all the routes?
>  ok in admin i have the following actions like login, logout, main,
> search and add_user so i have to explicitly state all of them? like
> this?
>
> map.connect "burninglegion/admin/login",
>              :controller => "admin",
>              :action => "login"
>
> map.connect "burninglegion/admin/logout",
>              :controller => "admin",
>              :action => "logout"
>
> map.connect "burninglegion/admin/search",
>              :controller => "admin",
>              :action => "search"
>
> map.connect "burninglegion/admin/add_user",
>              :controller => "admin",
>              :action => "add_user"
>
> map.connect "burninglegion/admin/main",
>              :controller => "admin",
>              :action => "main"
>
> omg.... is there a more dynamic way??? cos i just wanna add my name in
> front of all the url only T-T

would this work?

map.connect "burninglegion/admin/:action",
             :controller => 'admin'
Bao L. (Guest)
on 2006-06-09 07:12
Ken K. wrote:
> would this work?
>
> map.connect "burninglegion/admin/:action",
>              :controller => 'admin'

:D yeah.... this worked for the admin controller only...ahaha

so i just duplicate it for other controllers like

map.connect "burninglegion/admin/:action", :controller => 'admin'
map.connect "burninglegion/blog/:action", :controller => 'blog'
map.connect "burninglegion/test/:action", :controller => 'test'

the controller name have to be explicit so that RoR will know which
controller you are referring to..

although it seems abit un dynamic but that;s the best i could do!...
thanks everyone for help! especially steven and ken
Ken K. (Guest)
on 2006-06-09 07:18
bao lee wrote:
> Ken K. wrote:
>> would this work?
>>
>> map.connect "burninglegion/admin/:action",
>>              :controller => 'admin'
>
> :D yeah.... this worked for the admin controller only...ahaha
>
> so i just duplicate it for other controllers like
>
> map.connect "burninglegion/admin/:action", :controller => 'admin'
> map.connect "burninglegion/blog/:action", :controller => 'blog'
> map.connect "burninglegion/test/:action", :controller => 'test'
>
> the controller name have to be explicit so that RoR will know which
> controller you are referring to..
>
> although it seems abit un dynamic but that;s the best i could do!...
> thanks everyone for help! especially steven and ken

Sorry, I forgot to tell you that you had to do that for every
controller.

However, have you tried doing this?

map.connect "burninglegion/:controller/:action/:id"

Now, I am completely unsure whether this would work, but worth a try.

I also forgot to tell you to add :id. That routes anything after the
action and passes it to your application as a params hash. Hence,
/burninglegion/admin/show/38 would mean that params[:id] == 38.
Bao L. (Guest)
on 2006-06-12 08:09
Ken K. wrote:
> bao lee wrote:
>> Ken K. wrote:
>>> would this work?
>>>
>>> map.connect "burninglegion/admin/:action",
>>>              :controller => 'admin'
>>
>> :D yeah.... this worked for the admin controller only...ahaha
>>
>> so i just duplicate it for other controllers like
>>
>> map.connect "burninglegion/admin/:action", :controller => 'admin'
>> map.connect "burninglegion/blog/:action", :controller => 'blog'
>> map.connect "burninglegion/test/:action", :controller => 'test'
>>
>> the controller name have to be explicit so that RoR will know which
>> controller you are referring to..
>>
>> although it seems abit un dynamic but that;s the best i could do!...
>> thanks everyone for help! especially steven and ken
>
> Sorry, I forgot to tell you that you had to do that for every
> controller.
>
> However, have you tried doing this?
>
> map.connect "burninglegion/:controller/:action/:id"
>
> Now, I am completely unsure whether this would work, but worth a try.
>
> I also forgot to tell you to add :id. That routes anything after the
> action and passes it to your application as a params hash. Hence,
> /burninglegion/admin/show/38 would mean that params[:id] == 38.


i tried that in the beginning and it would only work for the first page
.eg. the one you typed in... after that it wouldn't work for the
subsequent page... i dunno whether is my coding problem or wat....
Asa H. (Guest)
on 2006-06-12 22:18
It looks like you might want to redefine the relative URL root, with
something like this:

ActionController::AbstractRequest.relative_url_root = "/burninglegion"

I use this (in association with appropriate code in lighttpd.conf) to
run two rails apps on the same domain, using the first part of the URL
after the domain to distinguish between them (test.com/app1/user/login
vs. test.com/app2/user/login).

Asa
This topic is locked and can not be replied to.