Rails 3资产和CSS相对URLS

fgu*_*len 5 ruby-on-rails asset-pipeline ruby-on-rails-3.2

我有几个CSS文件使用相对路径来引用图像url( "../img/my_image.jpg" ).

一切都developmentproduction环境中工作,因为所有CSS文件都打包在一个文件中并且结构丢失,亲戚路径也丢失了,并且找不到图像.

我的结构细节

我有这样的资产结构:

/app
  /assets
    /plugins
      /my_plugin
        /img
          my_image.jpg
        /css
          my_css.css
Run Code Online (Sandbox Code Playgroud)

(/my_plugin可以是任何一组插件css,js以及images任何其他的Twitter Bootstrap文件)

进入/app/assets/plugins/my_plugin/my_css.css我有类似的东西:

background-image: url("../img/my_image.jpg");
Run Code Online (Sandbox Code Playgroud)

/app/assets/stylesheets/application.css:

*= require css/my_css.css
Run Code Online (Sandbox Code Playgroud)

最后在head我的html文件中:

<%= stylesheet_link_tag "application" %>
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

更新

Mini应用程序重现问题,在README中有安装和重现问题的指令.

jdo*_*doe 7

我没有看到连接CSS文件的问题.并且您不必手动包含深层嵌套的CSS文件:默认情况下,application.css已经在行中执行此操作*= require_tree .

图像将重复您的app/assets/images结构.要在开发和生产环境中正确处理它们,您可以使用相应的指南.查看2.2编码链接到资产的部分.有关编码图像文件路径的说明:

通过ERb的CSS:

 url(<%= asset_path 'image.png' %>)
Run Code Online (Sandbox Code Playgroud)

CSS通过Sass:

 image-url("rails.png")
Run Code Online (Sandbox Code Playgroud)

预编译图像末尾的所有这些奇怪的十六进制字符将自动受到尊重.

UPD

我发现根本不需要插件的资产管道功能.您的样式/脚本已经缩小.您可以将plugins文件夹移动到您的public文件夹中(成为<APP_ROOT>/public/plugins/).

然后从以下位置删除以下行application.css:

*= require bootstrap/css/bootstrap
Run Code Online (Sandbox Code Playgroud)

而是将以下行添加到您的模板application.html.erb:

  <%= stylesheet_link_tag    "/plugins/bootstrap/css/bootstrap.min.css", :media => "all" %>
  <%= javascript_include_tag "/plugins/bootstrap/js/bootstrap.min.js" %>
Run Code Online (Sandbox Code Playgroud)

现在,您必须能够通过替换public/plugins/bootstrap内容轻松切换主题.

UPD 2

也许你必须明确地告诉Rails的,以precompile你的插件资产:

# /config/environments/production.rb
config.assets.precompile += %w( bootstrap/css/bootstrap.css )
Run Code Online (Sandbox Code Playgroud)