Rails 静态页面路由 - 有更好的方法吗?

发布于 2024-09-30 20:44:20 字数 521 浏览 0 评论 0原文

澄清一下,情况如下:

我正在构建一个非常简单的 CMS,它将成为我计划在未来创建的多个应用程序的基础。我真的希望能够创建一个名为“About”的页面(大部分是静态的),并自动能够在“/about”访问它,而无需修改routes.rb。

我目前有一个像这样的通配符路线,效果很好:

match '/*slug', :to => 'pages#dynamic_page', 
                :constraints => { :fullpath => /.+\.html/ }

问题是,我真的希望能够省略末尾的“.html”。但是,与“/pages/about”之类的 url 相比,我更喜欢扩展名。有更好的方法来处理这个问题吗?当我删除约束时出现的问题是,对不存在的项目的请求会通过 Rails 路由器,这显然不太理想,因为这是必须处理的额外开销。

有更好的办法吗?如果请求的页面是静态页面,是否有某种方法可以完全避免路由器,这样我就可以消除通配符路由?

谢谢!

To clarify, here's the situation:

I'm building a really simple CMS that will become the base for several apps I have plans to create in the future. I'd really like to be able to create a page called "About" (which will be mostly static) and automatically be able to access it at "/about", without having to modify routes.rb.

I currently have a wildcard route like this, which works just fine:

match '/*slug', :to => 'pages#dynamic_page', 
                :constraints => { :fullpath => /.+\.html/ }

The issue is, I'd really like to be able to omit the ".html" at the end. However, I prefer the extension over a url like "/pages/about". Is there a better way to handle this? The issue that occurs when I remove the constraint is that requests to items that don't exist go through the Rails router, which is obviously less than desirable, as that's extra overhead that has to be handled.

Is there a better way? Is there some way to avoid the router entirely if the page being requested is a static page, so I can eliminate the wildcard route?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2024-10-07 20:44:20

要在 Rails 路由器中解决此问题,您应该能够将路由添加到 paths.rb 的最底部,如下所示:

match '/:slug(.:format)', :to => 'pages#dynamic_page'

括号将路由的 :format 参数标记为可选,因此 /about 或 /about .html 应该可以工作。

它需要位于路由文件的底部,这样就不会干扰您的其他路由。

如果您想避免使用 Rails 路由器,您有两个选择,两者都更高级一些。

  1. 在 Web 服务器的配置中,添加将 /about 映射到其他某个 URI 的重写规则。

  2. 添加 Rack 中间件或 Rails 金属来处理静态页面路由。这避免了通过整个 Rails 路由堆栈运行这些请求,但是 Rails 3 路由器非常快,我不确定是否值得为了提供半静态页面而添加如此多的复杂性。

您可能还想研究一下HighVoltagegem。它是一个主要提供静态页面的 Rails 引擎。默认情况下,它会为您提供一个 /pages/about 样式的 URL,但您可以将以下内容添加到您的路由中以使其更漂亮:

# High Voltage treats pages like a REST resource; the page's name is the ID 
match '/:id(.:format)', :to => 'high_voltage/pages#show'

To solve this in the Rails router, tou should be able to just add a route to the very bottom of routes.rb that looks something like this:

match '/:slug(.:format)', :to => 'pages#dynamic_page'

The parentheses mark the :format parameter of the route as optional, so /about or /about.html should work.

This needs to be at the bottom of the routes file, so that it won't interfere with your other routes.

If you want to avoid the Rails router, you have two options, both a little more advanced.

  1. In your web server's config, add a rewrite rule that maps /about to some other URI.

  2. Add a Rack middleware or Rails metal to handle your static page routes. This avoids running these requests through the whole Rails routing stack, but the Rails 3 router is pretty fast and I'm not sure it's worth it to add this much complexity just to serve a semi-static page.

You may also wanna look into the High Voltage gem. It's a Rails engine for serving up mostly static pages. By default it gives you a /pages/about style URL, but you could then add the following to your routes to make it prettier:

# High Voltage treats pages like a REST resource; the page's name is the ID 
match '/:id(.:format)', :to => 'high_voltage/pages#show'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文