gpt4 book ai didi

ruby-on-rails - Rails 供应商 Assets 未提供服务.. 未找到路由错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:14 25 4
gpt4 key购买 nike

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

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

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

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

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

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

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

编辑:

我正在阅读本教程

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

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

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

最佳答案

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

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

  2. 要仅向某些特定页面提供 Assets ,您需要实现某种逻辑来加载基于它的 Assets 。例如。 Controller 特定 Assets :check here.

选项 1 的更新

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

这是我的 View 表单的样子:

<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

Controller 方法说明:

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

css Assets 文件的编译和创建发生在创建/更新操作完成之后。 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 和 digest 的文件名。

现在您需要在每一页中选取文件。为此,在 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;
}

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

关于ruby-on-rails - Rails 供应商 Assets 未提供服务.. 未找到路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37315246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com