Rails 3.1 force .html instead of no extension

大憨熊 提交于 2019-12-05 01:51:19

问题


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"}


来源:https://stackoverflow.com/questions/8403826/rails-3-1-force-html-instead-of-no-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!