Rails 供应商资产未提供服务.. 找不到路由错误

Rails vendor assets not serving .. no route found error

我正在使用 ruby 2.2 和 rails 4.2。

在我的应用程序中有许多 CSS 和 JS 文件,我只想在 needed.But 时从服务器加载,当我使用样式表从供应商的独立文件夹中调用样式表时 link 标签我没有路由匹配错误

ActionController::RoutingError (No route matches [GET] "/vendor/theme/assets/stylesheets/application.css"):

application.css 是我单独的清单文件和包含许多 css 文件的文件夹 vendor/theme/assets/stylesheets/

我已经尝试将路由添加到 "Rails.application.config.assets.paths" 但仍然无法正常工作。

我尝试使用 public 文件夹来达到同样的目的,但仍然无法正常工作。

是否可以在不预编译这些资产的情况下为它们提供服务,因为只有一些单独的页面需要这些 assets.Kindly 建议。

编辑:

我正在学习本教程

http://railscasts.com/episodes/279-understanding-the-asset-pipeline?autoplay=true

资产文件夹中的资产工作正常 http://localhost:3000/assets/application.css

但是http://localhost:3000/vendor/theme/assets/stylesheets/application.css给出了找不到路由的错误

  1. 从您上面发布的代码来看,您似乎在尝试 在 Rails 应用中实施 css 主题。如果你,这就是我的方式 在我的应用程序中实现了主题功能。

    每当管理员更改主题文件并更新它时,一个 已编译的 css 文件在 generated/updated public/assets/themes/ 主题名称的文件夹。那个文件是 由应用程序根据当前应用的主题选取。 (我可以 如果这是您要查找的内容,请提供代码。)

  2. 要仅将资产提供给某些特定页面,您需要实现某种基于它加载资产的逻辑。例如。控制器特定资产:check here.

选项 1 更新

我有主题资源,其中保存了主题名称和两种主题颜色(您可以使用更多)。

这是我的视图表单的样子:

<div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
</div>
<div class="field">
   <%= f.label :color1 %><br>
   <%= f.text_field :color1 %>
</div>
<div class="field">
    <%= f.label :color2 %><br>
    <%= f.text_field :color2 %>
</div>

这是我的 ThemesController 文件的样子:

class ThemesController < ApplicationController
  after_action :compile_theme, only: [:create, :update]
  THEME_PATH = 'app/assets/'
  PATH = THEME_PATH + '/themes'

  def new
    @theme = Theme.new
  end

  def edit
    @theme = Theme.find(params[:id])
  end

  def create
    @theme = Theme.new(theme_params)

    if @theme.save
      # Create a scss theme file
      write_theme_file

      redirect_to @theme, notice: 'Theme was successfully created.'
    else
      render :new
    end
  end

  def update
    @theme = Theme.find(params[:id])

    if @theme.update(theme_params)
      # Create/Update a scss theme file, you can check for file exists 
      write_theme_file
      redirect_to @theme, notice: 'Theme was successfully updated.'
    else
      render :edit
    end
  end

  private
    def write_theme_file
      file = PATH + name + '.scss'
      body = "$color1: #{@theme.color1};\n$color2: #{@theme.color2};"
      File.write(file, body)
    end

    def compile_theme
      file = PATH + name + '.scss'

      theme_body = ''
      if File.exists?(file) && File.exists?(THEME_PATH + 'theme.scss')
        file = File.open(file)
        theme_body = file.read
        file.close

        file = File.open(THEME_PATH + 'theme.scss')
        theme_body = theme_body + file.read
        file.close
      else
        colors = ''
      end

      env = if Rails.application.assets.is_a?(Sprockets::Index)
        Rails.application.assets.instance_variable_get('@environment')
      else
        Rails.application.assets
      end

      Dir.mkdir(Rails.root + 'public/assets/themes') unless Dir.exists?(Rails.root + 'public/assets/themes')
      asset_file = File.join(Rails.root, 'public', asset_path(name))
      File.delete(asset_file) if File.exists?(asset_file)

      body = ::Sass::Engine.new(theme_body, {syntax: :scss, cache: false, read_cache: false, style: :compressed}).render
      File.write(File.join(Rails.root, 'public', asset_path(name)), body)
    end

    def asset_path(name)
      digest = Digest::MD5.hexdigest(name)
      "assets/themes/#{name}-#{digest}.css"
    end

    def name
      return @theme.name.downcase
    end

    def theme_params
      params.require(:theme).permit(:name, :color1, :color2)
    end
end

控制器方法说明:

创建或更新新主题时,它会存储主题并在 app/assets/themes 中创建一个新的 .scss 文件,其中包含主题名称和定义的颜色值。

css 资产文件的编译和创建发生在 create/update 操作完成之后。 compile_theme 方法查找 theme.scss (示例在底部)文件(我已经在 app/assets/stylesheets/ 文件夹中创建了基本主题颜色变量)并替换 $color1 和 $color2 变量使用当前主题文件中的颜色值。结果 css 数据保存在 theme_body 变量中。

body = ::Sass::Engine.new(theme_body, {syntax: :scss, cache: false, read_cache: false, style: :compressed}).render
File.write(File.join(Rails.root, 'public', asset_path(name)), body)

最后两行将在 public/assets/themes 中创建一个新文件,其中包含生成的 theme_body css 内容和文件名以及 theme_name 和摘要。

现在您需要在每一页中选取文件。为此,在 application_controller.rb 文件中,定义此

before_filter :set_theme

private
  def set_theme
    @theme = Theme.first # or change theme according to params
  end

最后,您需要在布局文件中选取主题文件。所以,在 layouts/application.html.erb 文件中添加:

<% if @theme.present? %>
   <% digest = Digest::MD5.hexdigest(@theme) %>
   <%= stylesheet_link_tag asset_url("assets/themes/#{@theme}-#{digest}.css") %>
<% end %>

仅供参考,这是我的 theme.scss 文件的样子:

body {
  background-color: $color1;
}
#header-wrapper, footer {
  background-color: $color2;
}

就是这样。希望这可以帮助。如果您有任何问题,请告诉我。