[英]ActionController::RoutingError (No route matches [GET] "/assets/javascripts
I am running a development server and I'm trying to load some static pages and files (images, css and javasripts) in my rails application for testing. 我正在运行开发服务器,并且尝试在Rails应用程序中加载一些静态页面和文件(图像,css和javasripts)以进行测试。 (In general, I'm new in rails. The Rails installation has been done on Linux subsystem in Windows 10).
(通常,我是Rails的新手。已经在Windows 10的Linux子系统上完成了Rails的安装)。 Although I have placed the files in
assets/images
, assets/javascripts
and assets/stylesheets
I couldn't manage to read these files in my test apprication when server runs. 尽管我已将文件放置在
assets/images
, assets/javascripts
和assets/stylesheets
中,但是当服务器运行时,我无法在测试应用程序中读取这些文件。 I receive the following errors almost for all files I have added: 对于添加的几乎所有文件,我都会收到以下错误:
ActionController::RoutingError (No route matches [GET] "/assets/javascripts/jquery.min.js"):
ActionController::RoutingError (No route matches [GET] "/assets/javascripts/bootstrap.min.js"):
ActionController::RoutingError (No route matches [GET] "/assets/javascripts/jqBootstrapValidation.js"):
and for images 和图像
ActionController::RoutingError (No route matches [GET] "/assets/images/portfolio/dreams-preview.png"):
The same applies for some css files also (not all). 同样的情况也适用于某些css文件(并非全部)。
I tried several ideas I found here in order to solve this but I had no luck. 为了解决这个问题,我尝试了一些在这里找到的想法,但是我没有运气。 I tried to setup:
我尝试设置:
config.serve_static_files = true
in development.rb
and 在
development.rb
和
rake assets:precompile
Obviously, these files are not visible by rails application but I still don't understand what I have to do to make them visible. 显然,这些文件对于Rails应用程序是不可见的,但是我仍然不知道如何使它们可见。 Can you please help me on this?
你能帮我吗?
You need to include these files in application.js manully or just write //= require_tree .
您需要手动将这些文件包含在application.js中,或者只写
//= require_tree .
to load all js 加载所有js
Similarly, you have to work for css. 同样,您必须为CSS工作。 Add
*= require_tree .
添加
*= require_tree .
in application.css
. 在
application.css
。
Finally, in order to resolve this error and transfer my static pages to Rails I had to modify my homepage.html.erb
as follows: 最后,为了解决此错误并将静态页面转移到Rails,我必须如下修改
homepage.html.erb
:
Stylesheet links : 样式表链接 :
<%= stylesheet_link_tag "bootstrap.min.css" %>
the following also works: 以下内容也适用:
<link href="assets/bootstrap.min.css" rel="stylesheet">
Javascript include : JavaScript包括 :
<%= javascript_include_tag "bootstrap.min.js" %>
In general, I found that no precompilation is necessary. 通常,我发现没有必要进行预编译。 I tested and it works with or without precompilation.
我进行了测试,无论是否进行预编译,它都可以工作。 The following also works:
以下内容也适用:
<script src="assets/bootstrap.min.js"></script>
Tag for images : 图片标签 :
<%= image_tag("portfolio/dreams-preview.png", class: "img-responsive img-centered") %>
Rails use by default the normal path for images ( assets/images
). 默认情况下,Rails使用图像(
assets/images
)的常规路径。 The full path should not be used. 不应使用完整路径。 Unless the path for images is different, use only the path deeper than
assets/images
, if exists. 除非图像的路径不同,否则仅使用比
assets/images
更深的路径(如果存在)。
No changes were made in application.html.erb
but precompilation for javascripts and stylesheets is necessary in assets.rb
: 在
application.html.erb
中未进行任何更改,但在assets.rb
需要对javascript和样式表进行预编译:
Rails.application.config.assets.precompile += ['agency.min.js', 'agency.min.css', ...]
Eventually the site works, although some conflicts in the execution of javascripts might be appear. 最终该站点可以运行,尽管在执行JavaScript时可能会出现一些冲突。
I was actually getting these kind of errors too. 我实际上也遇到了这类错误。 Mine looked like this:
我的看起来像这样:
F, [2018-06-16T17:46:02.121588 #6963] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/assets/theme/bootstrap.min.css"):
[2018-06-18T12:55:20.169664 #18951] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/assets/theme/main.css"):
And I never added any reference to these files in my application. 而且我从未在我的应用程序中添加对这些文件的任何引用。 So I did not immediately realize why these specific css files were being requested.
所以我没有立即意识到为什么要请求这些特定的CSS文件。 Well, it happens that after my application received a 404 error or 500 error, it would load the corresponding 404.html or 500.html in the public directory.
好吧,碰巧我的应用程序收到404错误或500错误后,它将在公共目录中加载相应的404.html或500.html。 Those directories did reference these css files:
这些目录确实引用了以下css文件:
<head>
<title>PAGE NOT FOUND (404)</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/assets/theme/bootstrap.min.css">
<link rel="stylesheet" href="/assets/theme/main.css">
</head>
So remove the references here and errors will go away. 因此,请删除此处的参考,错误将会消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.