using map.root on rails to map to a static path
I'm using static URLs in my RoR project. There is really no controller or action to map to so I tried putting in map.root "/static_page"
or map.root "static_page"
but I get errors. How do I just map to a path?
Edit: I am using a controller to serve up stat开发者_运维知识库ic pages, these aren't html pages, but a static controller I'm using here: http://snafu.diarrhea.ch/blog/article/4-serving-static-content-with-rails
I do not think that map.root does what you want it to do.
map.root looks like a named route because it is. But it's a special named route for the routing of a url without a local path (eg: 'http://www.example.com/'). So it doesn't need a path argument like standard named routes.
It looks like you're either unaware of what map.root does, or are trying to shoehorn the root mapping into your custom static controller.
If it's the former, just choose another name for your route to solve your problem. If it's the later, all you really have to do is pass a specific path value. Place it above your catch all for static pages or else it will never match.
map.root :controller => 'static', :path => "my_template.html"
If you were trying to force a specific route to go to a specific static page, creating a file with that path descending from public will by pass routes entirely. So you don't even need to write a route. (eg: http://www.example.com/whatever/static_pages
automatically serves public/whatever/static_pages
if it exists)
However, if you want that file to contain erb/haml/etc, you want to use the method above, to route the request through your static controller. The question says that it doesn't map to a controller/action, but by creating the static controller you do have a route and now an action for your named route. If for whatever reason the path doesn't fit your controller logic, you can use the explicit_path action defined above to provide the desired path internally as a parameter with a route like this.
map.static_page '/static_page', :controller => 'static',
:path => "my template.html"
P.S.: template_exist has been deprecated in Rails 2.3. So you will have to define it yourself if you upgrade.
You don't need a route. Just put the file in /public and it should work
精彩评论