简体   繁体   English

Rails 3 response_to:html并防止出现双重内容

[英]Rails 3 respond_to :html and preventing double content

I'm trying to prevent double content in combination with respond_to. 我正在尝试防止与response_to结合使用双重内容。 So I added to my controller: 所以我添加到我的控制器:

respond_to :html

In combination with respond_with this works great so far, butt he user can still access my pages using either: 到目前为止,与respond_with结合使用效果很好,但是他的用户仍然可以使用以下任一方法访问我的页面:

  • /my/page /我的页面
  • /my/page/ or / my / page /或
  • /my/page.html /my/page.html

I'd like to limit this to one of them (preferring the first one). 我想将其限制为其中一个(最好是第一个)。 Removing formats fully is not a solution, because I might like to respond_with json or xml someday. 完全删除格式不是解决方案,因为有一天我可能希望使用json或xml来响应。

Thank you in advance for any tip! 预先感谢您的提示!

Doing this will be pretty unpleasant and will also make your site more difficult for users to, well, use. 这样做将非常令人不快,也将使用户更难以使用您的网站。 Unless there's a really, really compelling reason for you to do this, I would just work with the Rails defaults and accept the fact that you have a lot of different URLs pointing to one resource. 除非有确实非常令人信服的理由来执行此操作,否则我将使用Rails默认值并接受以下事实:您有许多不同的URL指向一个资源。 Doing so really shouldn't hurt you. 这样做确实不会伤害您。

Again, you should probably not do this. 同样,您可能不应该这样做。 But if you really wanted to, you could play with routes.rb to manually create the routes you wanted. 但是,如果您确实需要,可以使用routes.rb手动创建所需的路由。 Check out the Rails routing guide for more information on how to generate Rails routes that look more like what you want. 查看Rails路由指南 ,以获取有关如何生成看起来更像您想要的Rails路由的更多信息。

That said, that still probably won't be enough to get rid of my/page/ and my/page pointing to the same place. 就是说,这仍然可能不足以摆脱my / page /和my / page指向同一位置。 If that is really, truly necessary, I would consider dropping Rails entirely and using a different framework like Sinatra, where you have very fine-grained control over routing (since the framework makes no routing assumptions at all). 如果确实确实需要这样做,我会考虑完全删除Rails,并使用诸如Sinatra这样的不同框架,在该框架中您可以非常精细地控制路由(因为该框架根本没有任何路由假设)。

But the best thing to do is probably just accept the routes as they are and move on. 但是最好的办法可能是仅接受原路并继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM