在Rails中的ruby中包含自定义文件夹特定的资产 [英] Include custom folder specific assets in ruby on rails
问题描述
我正在尝试创建单独的文件夹,以保留与后端和前端相关的资产.我计划了以下文件夹结构:app/assets/dashboard/javascripts
而不是通常的app/assets/javascripts
.
I am trying to create separate folders for keeping the assets related to backend and frontend. I planned the following folder structure: app/assets/dashboard/javascripts
instead of the usual one app/assets/javascripts
.
后端通过app/views/layouts/dashboard.html.erb
提供服务,并通过以下方式包含资产:
Backend is served via app/views/layouts/dashboard.html.erb
, and includes the assets in the following way:
<%= stylesheet_link_tag 'dashboard/dashboard', params[:controller], media: 'all' %>
<%= javascript_include_tag 'dashboard/dashboard', params[:controller] %>
前端通过app/views/layouts/application.html.erb
提供服务,并通过以下方式包括资产:
Front end is served via app/views/layouts/application.html.erb
, and includes the assets in the following way:
<%= stylesheet_link_tag 'store/application', params[:controller], media: 'all' %>
<%= javascript_include_tag 'store/application', params[:controller] %>
I am not sure that what is the correct way to include the assets in this way, and hence getting 404 not found
error for http://dashboard.localhost.com:3000/javascripts/dashboard/javascripts/dashboard.js in the console
我也尝试过将脚本路径更改为<%= javascript_include_tag 'store/javascripts/application', params[:controller] %>
,但是它也不起作用.
还尝试添加此处中提到的以下代码到application.rb,但它也不起作用:
I've also tried changing the scripts path to <%= javascript_include_tag 'store/javascripts/application', params[:controller] %>
, but it doesn't work either.
Also tried adding the following piece of code mentioned here to application.rb, but it didn't work either:
Dir.glob("#{Rails.root}/app/assets/**/").each do |path|
config.assets.paths << path
end
使用这些设置,控制台中填充的路径如下:
With these settings the paths populated in the console are as follows:
[9] pry(main)> Rails.application.config.assets.paths
=> ["/home/vipin8169/projectRoot/inmonarch_website/app/assets/images",
"/home/vipin8169/projectRoot/xyz/app/assets/javascripts",
"/home/vipin8169/projectRoot/xyz/app/assets/stylesheets",
"/home/vipin8169/projectRoot/xyz/vendor/assets/javascripts",
"/home/vipin8169/projectRoot/xyz/vendor/assets/stylesheets",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/jquery-rails-4.1.0/vendor/assets/javascripts",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/coffee-rails-4.1.1/lib/assets/javascripts",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/react-rails-1.6.2/lib/assets/react-source/development-with-addons",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/react-rails-1.6.2/lib/assets/javascripts/",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/bootstrap-sass-3.3.6/assets/stylesheets",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/bootstrap-sass-3.3.6/assets/javascripts",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/bootstrap-sass-3.3.6/assets/fonts",
"/home/vipin8169/.rvm/gems/ruby-2.2.3@rails426/gems/bootstrap-sass-3.3.6/assets/images"]
它是否受Rails支持,以这种方式更改资产目录结构?
Is it supported in rails, altering the assets directory structure in this way?
推荐答案
在application.rb文件中.添加
In your application.rb file. add this
config.assets.enabled = true
config.assets.enabled = true
config.assets.paths<< Rails.root.join("app","assets","dashboard")
config.assets.paths << Rails.root.join("app", "assets", "dashboard")
它将在资产路径下添加此文件夹.之后
It would add this folder under the assets path. After that
stylesheet_link_tag'dashboard',params [:controller],media:'all'
stylesheet_link_tag 'dashboard', params[:controller], media: 'all'
会工作的.
这篇关于在Rails中的ruby中包含自定义文件夹特定的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!