简体   繁体   中英

Rails 3 respond_to :html and preventing double content

I'm trying to prevent double content in combination with respond_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:

  • /my/page
  • /my/page/ or
  • /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.

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. 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. Check out the Rails routing guide for more information on how to generate Rails routes that look more like what you want.

That said, that still probably won't be enough to get rid of my/page/ and my/page pointing to the same place. 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).

But the best thing to do is probably just accept the routes as they are and move on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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