开发者

Routing Question in Rails

How would i go about routing www.homepage.com/site/about to www.homepage.com/about for example? I still would like to use "site" as a controller, I just want to remove it from the route.

On a separate note, ss this how you usual开发者_如何学JAVAly setup routes for pages like about and contact that appear right after the site name?


For the first question, you could put something like this in your routes.rb:

map.about '/about', :controller => 'site', :action => 'about'

As for the second question, i don't quite undestand it, can you be a little more specific?


On a separate note, ss this how you usually setup routes for pages like about and contact that appear right after the site name?

If you follow the principles of REST (and you probably should), then by and large every URL should be terminating in a "resource" (ie: a noun, a "thing"). Each resource in turn has a controller, and these controllers have a standardized and limited set of actions.

This is a bit different from the "classic" routing scheme, where a controller would have more varied actions (and thus you could lump more functionality into a single, larger controller)

So, for example, in a RESTful system:

  • /contact would map to a ContactController, and not a "contact" action in some other controller (as it could in classic routing)
  • /about would similarly map to an AboutController.
  • /site/about would map to an AboutController, but this would be a "nested" route under the Site namespace. If you want to get hardcore about the nesting (and I typically do), I would:
    • Put AboutController in the Site module (thus Sites::AboutController), which in turn is stored in the file /app/controllers/site/about_controller.rb
    • Create a SiteController
    • Map /site to SiteController

If you had a preexisting Site:AboutController mapped to /site/about and you wanted to remap it to /about, you could do so using @rbaezam79's method. However, I'd seriously consider relocating and renaming the class itself just for consistency.

All of the resources I listed above would probably be considered "singleton" resources. This means:

  • you use map.resource instead of map.resources to map your routes
  • the default action is "show" (and not "index" as it is with non-singleton resources). Typically this will be the only action you need. (You wouldn't ever want to create or delete your about page, would you?)
  • the names are typically singular instead of plural. (Although I've run into some stumbling blocks with this)

When setting up routes like this, be sure to run the command "rake routes" regularly; this will show you exactly what you're getting at any given time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜