Rails 3.1 force .html instead of no extension

2019-03-31 13:31发布

问题:

One of my clients wants his new Rails application to look more like his traditional web site. He wants to know if I can force urls to have a file extension, preferably .html.

I don't want to hard-code the extension in routes.rb as

match ':controller/:action/:id.html'

(or similar) because the client also wants to have a respond_to-style JSON API which requires the use of .:format.

Can this be done?

回答1:

Just as Mattias Wadman suggested, in config/application.rb add:

AppName::Application.default_url_options = { :format => "html" }

But also change config/routes.rb to:

root :to => 'pages#home', :defaults => { :format => "html" }


回答2:

Im no Rails routing expert but I tried to force HTML format by changing the default URL options and at least the URL helpers seams to generate .html URLs now, it's a start.

config/application.rb (at the bottom)

AppName::Application.default_url_options = {:format => "html"}