Rails 3.1强制使用.html而不是没有扩展名 [英] Rails 3.1 force .html instead of no extension
本文介绍了Rails 3.1强制使用.html而不是没有扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的一位客户希望他的新Rails应用程序看起来更像他的传统网站。他想知道我是否可以强制url具有文件扩展名,最好是 .html
。
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
.
我不知道想要在 routes.rb
中将扩展名硬编码为
I don't want to hard-code the extension in routes.rb
as
match ':controller/:action/:id.html'
(或类似),因为客户端也想要具有 respond_to
样式的JSON API,该API需要使用 .: format
。
(or similar) because the client also wants to have a respond_to
-style JSON API which requires the use of .:format
.
可以做到吗?
推荐答案
就像Mattias Wadman所建议的那样,在<$ c $中c> config / application.rb 添加:
Just as Mattias Wadman suggested, in config/application.rb
add:
AppName::Application.default_url_options = { :format => "html" }
还要更改 config / routes.rb
到:
root :to => 'pages#home', :defaults => { :format => "html" }
这篇关于Rails 3.1强制使用.html而不是没有扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文