Ruby on Rails。 Unicode路由 [英] Ruby on Rails. Unicode routes

查看:71
本文介绍了Ruby on Rails。 Unicode路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Unicode字符串设置为Rails中路径的一部分?

Is it possible to set a Unicode string as a segment of a path in Rails?

我尝试以下操作:


  
# app/controllers/magazines_controller.rb

class MagazinesController < ApplicationController
  def index                                     
  end                                                                           
end
  




  
# encoding: utf-8
# config/routes.rb

PublishingHouse::Application.routes.draw do
  resources :magazines,
    :only => :index,
    :path => :журналы # a Unicode string is set as a segment of the path
end
  




$ rake routes
magazines GET /журналы(.:format) {:action=>"index", :controller=>"magazines"}

但是当我转到路径时,会收到路由错误:

But when I go to the path I get the Routing error:


$ w3m http://localhost:3000/журналы
...

Routing Error

No route matches "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B"

这是服务器日志:


$ rails s thin
...

Started GET "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B" for 127.0.0.1 at 2010-09-26 13:35:00 +0400

ActionController::RoutingError (No route matches "/%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB%D1%8B"):

Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)

谢谢。

Debian GNU / Linux 5.0.6;

Debian GNU/Linux 5.0.6;

Ruby 1.9.2;

Ruby 1.9.2;

Ruby on Rails 3.0.0。

Ruby on Rails 3.0.0.

推荐答案

有趣的是,我认为Rails需要一个为此打补丁。稍后我将与核心人员讨论。同时,以下方法应该起作用:

Intereting, I think Rails need a patch for this. I shall speak with someone from core about it later. In the meantime, the following should work:

PublishingHouse::Application.routes.draw do
  resources :magazines,
    :only => :index,
    :path => Rack::Utils.escape('журналы') # a Unicode string is set as a segment of the path
end

这篇关于Ruby on Rails。 Unicode路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆