Rails 应用 returns 错误中的嵌入式 swf 文件

Embedded swf file in Rails app returns error

我是 Rails 的新手,我在让我的 swf 文件在我的 rails 应用程序中正常工作时遇到了问题。当我单独打开时,我的 swf 文件可以正常工作。当我将它嵌入随机 html 文件时,它也能正常工作。

现在,我将我的 swf 文件 'game.swf' 嵌入到 'hello.html.erb',我的 'app/views' 文件夹中的文件之一:

<embed src="<%= asset_path('game.swf') %>"
       type="application/x-shockwave-flash"
       allowScriptAccess="always" />

我的 swf 文件加载正常,但是当它必须加载文本文件时出现问题,'map1.txt' 在我的 swf 文件中,我有一个 URLLoader,它应该用这个相对路径加载一个文本文件:

"../txts/maps/map1.txt"

这是 rails 应用程序中我的资产文件夹的一部分:

--assets
----swfs
------game.swf
----txts
------maps
---------map1.txt

当我在 Rails 应用程序中播放 swf 文件时,出现 returns 错误 #2032...流错误。 好像我的 swf 文件找不到 map1.txt.

我已经尝试使用此处提供的解决方案以不同方式嵌入 swf 文件 https://developers.google.com/interactive-media-ads/docs/sdks/flash/v3/errorcodes,但我的 swf 文件仍然出现同样的错误。

我认为问题可能与 Rails 资产管道有关。我在这里读 http://guides.rubyonrails.org/asset_pipeline.html Rails 将资产放在一个全新的文件夹中,例如 public/assets/,因此一旦我运行该应用程序,文件的特定路径(如上面的路径)将不再有效。如果是这样,我如何修改我的 swf 文件中的路径,以便它可以正确引用我的 map1.txt 文件?

或者...我的第二个猜测是 Rails 不知道我的嵌入式 swf 需要 txt 文件,所以 rails 不会加载 txt 文件。我是否必须修改我的 Rails 应用程序或执行任何操作,以便 Rails 知道我的 swf 文件需要访问我的 map1.txt 文件?

基本上,我认为问题在于我无法通过 Rails 中的另一资产访问一项资产。有什么解决办法吗?

谢谢!

再次编辑:

这是 cmd 的内容:

Started GET "/assets/game.swf" for 127.0.0.1 at 2015-01-04 15:32:08 +0800


Started GET "/assets/map1.txt" for 127.0.0.1 at 2015-01-04 15:32:15 +0800

ActionController::RoutingError (No route matches [GET] "/assets/map1.txt"):
  actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `c
all'
  actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `ca
ll'
  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 tag
ged'
  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'
  c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `servi
ce'
  c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in
start_thread'


  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/routes/_table.html.erb (28.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within
rescues/layout (148.0ms)

简单的答案是从 /public 文件夹服务器静态资产。您可以将 .txt 文件放入

/public/txts/maps/map1.txt

然后在 .swf 文件中将其引用为

"/txts/maps/map1.txt"

Rails 将通过自动查找 /public... 文件夹来处理请求。由于这些是静态资产,因此应该可以正常工作。

Rails 不会阻止 .swf 文件发出请求。 .swf 文件在浏览器中 运行,因此浏览器只是向 Web 服务器发出绝对路径请求。

当我部署我的 rails 应用程序时,Beartech 将地图文件夹转移到 public/assets 的上述解决方案是迄今为止最好的解决方案。

但是,如果我只想在本地 运行 rails 应用程序,则不必将我的 txt 文件放在 /public 文件夹中。

swf 文件中的文件路径应设置为 "assets/maps/map1.txt",而不是我尝试过的任何这些:“../txts/maps/map1.txt”、"assets/map1.txt" 或只是 "map1.txt"。

控制台显示:

Started GET "/assets/game.swf" for 127.0.0.1 at 2015-01-04 19:30:00 +0800

Started GET "/assets/maps/map1.txt" for 127.0.0.1 at 2015-01-04 19:32:23 +0800

有趣的是,控制台没有提到子目录 swfstxts 的名称。 这是因为Rails在编译时将app/assets的子目录的内容全部转移到一个大文件夹public/assets/中。 Rails 然后在 public/assets/ 文件夹中搜索。

如果我使用 RAILS_ENV=production bin/rake assets:precompile 命令进行预编译,那么 我的 public/assets 文件夹将包含以下内容:

--public/assets
----game.swf
----maps
------map1.txt

这就是为什么文本文件的相对路径现在是 "maps/map1.txt"