【问题标题】:Embedded swf file in Rails app returns errorRails 应用程序中的嵌入式 swf 文件返回错误
【发布时间】:2015-01-04 06:22:39
【问题描述】:

我是 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 文件时,它返回错误 #2032...Stream 错误。 好像我的 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)

【问题讨论】:

  • 你能显示网络服务器控制台输出吗?另外,什么版本的 Rails?
  • 抱歉,我忘了放这个...我使用的是 Rails 4.1.8。
  • Web 控制台输出很重要,它应该显示服务器在抛出错误时正在做什么。
  • 所以很简单,控制器找不到文件。你能运行RAILS_ENV=production bin/rake assets:precompile 看看它是否能解决你的问题吗?
  • 好的,将 map1.txt 的副本放入 public/assets 文件夹,看看会发生什么。我认为您最终可能不得不将文件存储在 Public 文件夹中。

标签: html ruby flash ruby-on-rails-4 asset-pipeline


【解决方案1】:

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

/public/txts/maps/map1.txt

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

"/txts/maps/map1.txt"

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

Rails 没有阻止 .swf 文件发出请求。 .swf 文件是在浏览器中运行的,所以浏览器只是在向 Web 服务器发出绝对路径请求。

【讨论】:

    【解决方案2】:

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

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

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

    控制台显示:

    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"

    【讨论】:

    • 每次我认为我了解资产管道时,我都没有。看起来很简单。
    猜你喜欢
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多