Rails public 中的中文静态资源获取失败

cyzgbw · 2015年04月18日 · 最后由 cyzgbw 回复于 2015年04月21日 · 2325 次阅读

rails4.0.2,在 public 文件夹下面有中文文件,在开发环境中通过 url 获取此中文文件失败,换成英文则正常,使用的是 rails 自带服务器,请问这里的中文静态资源如何获取,谢谢。

可以考虑升级到 Rails 4.0.13 或者目前的最新版 4.2.1,还有 Ruby 2.1.6,至少我可以。

贴 Rails 日志

#2 楼 @huacnlee





Started GET "/font/%E4%BD%A0%E5%A5%BD.otf" for 127.0.0.1 at 2015-04-18 17:16:46 +0800
Started GET "/font/%E4%BD%A0%E5%A5%BD.otf" for 127.0.0.1 at 2015-04-18 17:16:46 +0800

ActionController::RoutingError (No route matches [GET] "/font/%E4%BD%A0%E5%A5%BD.otf"):
  actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.8) lib/rails/engine.rb:514:in `call'
  railties (4.1.8) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'



ActionController::RoutingError (No route matches [GET] "/font/%E4%BD%A0%E5%A5%BD.otf"):
  actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.8) lib/rails/engine.rb:514:in `call'
  railties (4.1.8) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  E:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/routes/_route.html.erb (4.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (124.0ms)
  Rendered E:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (124.0ms)

中文文件名称叫:你好.otf,public/font/你好.otf http://127.0.0.1:3000/font/%E4%BD%A0%E5%A5%BD.otf 【No route matches】 http://127.0.0.1:3000/font/FontAwesome.otf 【弹出下载框】

#1 楼 @ericguo 我试了,ruby-2.0.0,rails4.1.8,还是无法打开中文文件

为啥要用中文的啊?

#5 楼 @MrPasserby 用户上传的附件里面就有中文,paperclip 上传后就是放在 public 下面的

用 params 传文件名 然后 send_file 就行了

#7 楼 @long_chn 这个我知道,以前就是这么做的。但是还是想知道如何通过 url 直接去访问

#1 楼 @ericguo 你的 rails 和 ruby 版本是多少啊?

No route matches [GET] "/font/%E4%BD%A0%E5%A5%BD.otf

字体路径不对,LOG 里面不是很清楚吗?

哦,你已经放 public 里了,把中文文件名改为英文呢?

#10 楼 @libuchao 中文改成英文就可以下载。我知道显示的是路径不对,但是这个文件是实际存在的,在 chrome 里看 url 显示的也是"你好.otf",说明转码也是对的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号