【问题标题】:Rails vendor assets not serving .. no route found errorRails 供应商资产不提供服务.. 找不到路由错误
【发布时间】:2016-05-19 06:17:48
【问题描述】:

我正在使用 ruby​​ 2.2 和 rails 4.2。

在我的应用程序中有许多 CSS 和 JS 文件,我只想在需要时从服务器加载。但是,每当我使用样式表链接标记从供应商的独立文件夹中调用样式表时,我都没有收到路由匹配错误

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

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

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

我尝试将公用文件夹用于相同目的,但仍然无法正常工作。

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

编辑:

我正在阅读本教程

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 给出了找不到路由的错误

【问题讨论】:

  • 尝试将资产文件夹放在主题文件夹之外,EX。 :http://localhost:3000/vendor/assets/stylesheets/application.css
  • 只有当我将我的 css 和 js 放在默认的“vendor/assets”文件夹中时它才能工作。有没有其他方法可以从 vendor 的其他文件夹中提供资产?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems


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

    每当管理员对主题文件进行更改并更新它时, 编译的 css 文件在 带有主题名称的 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 资产文件的编译和创建发生在创建/更新操作完成之后。 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;
}

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

【讨论】:

  • 嘿,感谢您的回复。我正在寻找类似您发布的第一个选项的内容。如果您能描述一下您是如何让它为您工作的,那将会非常有帮助。
  • 非常感谢。你的上帝\m/
  • 很高兴我能在这里提供帮助!
  • 嘿,我在供应商文件夹中查看文件,并且在本地一切正常,但是在使用 capistrano 在 ec2 实例上部署时,应用程序中显示了“类的未定义方法 csrf 令牌”的错误。 html.erb 文件,如果我删除 csrf 令牌助手,则 application.html.erb 会显示相同的“没有给定块(yield)”错误的方法。有什么想法吗?
  • 不确定这个问题。可能是一个不同的问题?可能检查生产日志以获取更多信息会有所帮助。
猜你喜欢
  • 2016-10-23
  • 1970-01-01
  • 2014-03-11
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多