Rails 3:路线中的变音符号(或其他 unicode)
我有一个与 Ruby on Rails 类似的问题。 Unicode 路线 但标记为在那里工作的答案对我不起作用。
我想要一个包含元音变音 (ä) 的着陆页的基本路线。它必须与 SEO 目的完全相同。
# encoding: UTF-8
Udb::Application.routes.draw do
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
当我在浏览器中输入 http://localhost/bonitätsauskunft
时,出现路由错误 No Route matches "/bonit%c3%a4tsauskunft"
。因此,查询字符串在与路由匹配之前不会被转义,我认为这很糟糕,因为有多种方法可以对 URL 中的变音符号进行编码,而我不知道浏览器使用哪一种。
例如 CGI.escape("bonitätsauskunft") # => “bonit%C3%A4tsauskunft”
,注意大写的 C3 和 A4,而不是 Firefox 发送的 c3 和 a4。
因此 get CGI.escape("bonitätsauskunft")
和 Rack::Utils.escape("bonitätsauskunft")
都不匹配。
我也尝试过,但没有运气:
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :page => /bonitätsauskunft/
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :constraints => {:page => /bonitätsauskunft/}
唯一对我有用的是尴尬:
# encoding: UTF-8
class UmlautConstraint
def initialize(page)
@page = page
end
def matches?(request)
request.params[:page] == @page
end
end
Udb::Application.routes.draw do
get ":page" => "landing_pages#credit_reference", :constraints => UmlautConstraint.new("bonitätsauskunft")
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
当然,需要第二条路线,因此我可以使用命名路线来创建链接,例如 link_to("Bonitätsauskunft", :lp_credit_reference)
因为 Rails 不知道如何满足 UmlautConstraint。
难道没有一个正常且简单的方法可以做到这一点吗?我真的认为 Rails 3 现在国际化会更好。
Rails 3.0.10、Ruby 1.9.2、Apache2 和乘客 3.0.7。
I have a similar question as Ruby on Rails. Unicode routes but the answer marked as working there does not work for me.
I want to have a basic route for a landing page that contains an umlaut (ä). It has to be exactly like that for SEO purposes.
# encoding: UTF-8
Udb::Application.routes.draw do
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
When I enter http://localhost/bonitätsauskunft
in my browser, I get a routing error No route matches "/bonit%c3%a4tsauskunft"
. So the query string is not unescaped before matching with the route, which I think is bad, because there are multiple ways to encode umlauts in URLs and I cannot know which one the browser uses.
for example CGI.escape("bonitätsauskunft") # => "bonit%C3%A4tsauskunft"
, note the capital C3 and A4 instead of c3 and a4 like Firefox sends.
So both get CGI.escape("bonitätsauskunft")
and Rack::Utils.escape("bonitätsauskunft")
do not match.
I also tried with no luck:
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :page => /bonitätsauskunft/
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :constraints => {:page => /bonitätsauskunft/}
The only thing that works for me is the awkward:
# encoding: UTF-8
class UmlautConstraint
def initialize(page)
@page = page
end
def matches?(request)
request.params[:page] == @page
end
end
Udb::Application.routes.draw do
get ":page" => "landing_pages#credit_reference", :constraints => UmlautConstraint.new("bonitätsauskunft")
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
The second route, of course, is needed so I can use a named route to create links like link_to("Bonitätsauskunft", :lp_credit_reference)
because Rails would not know how to satisfy the UmlautConstraint.
Isn't there a normal and easy way to do this? I really thought rails 3 would be better with internationalization by now.
Rails 3.0.10, Ruby 1.9.2, Apache2 with passenger 3.0.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用您的第一个想法
CGI.escape("bonitätsauskunft").downcase
它适用于任何浏览器。它不依赖于浏览器编码,而是依赖于服务器编码。当您使用另一台服务器 (webrick) 时,可能需要更改路由。
我已经成功使用这种类型的“unicode”路由一段时间了,它确实具有良好的SEO效果。
Use your first idea with
CGI.escape("bonitätsauskunft").downcase
It will work with any browser. It don't depend on browser encoding, but SERVER encoding. When you use another server (webrick), routes could need to be changed.
I'm using this type of "unicode" routes successfully for some time and it really has good SEO effect.