在Ruby on Rails中加载CSS和JS文件

时间:2021-12-26 09:47:29

I have recently been converted to the Ruby on Rails side so I am new to the art of the asset pipeline and ruby on rails in general. The problem that I am having at the moment is that when I try to load my ruby on rails page up it is getting a 404 error on my default.js and my default.css. These two files are in the directory app/assets/javascripts and app/assests/stylesheets respectively.

我最近已经转换为Ruby on Rails方面,所以我对资产管道的艺术和一般的rails上的ruby都是新手。我目前遇到的问题是,当我尝试在rails页面上加载我的ruby时,我的default.js和我的default.css上出现404错误。这两个文件分别位于app / assets / javascripts和app / assests / stylesheets目录中。

I believe that this problem is caused by my stylesheet_link_tags and javascript_link_tags inside my application.html.erb. Below you can see my application.html.erb

我相信这个问题是由我的application.html.link中的stylesheet_link_tags和javascript_link_tags引起的。您可以在下面看到我的application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Loadtest</title>
  <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width = device-width, initial-scale = 1">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Ubuntu">

</head>
<body>
<div id = "content">
    <%= yield %>
</div>


</body>
</html>

At the beginning the link tags had application after them but this SO Question led me to believe that changing them to default would fix the problem.

在开始时,链接标签在它们之后有应用程序,但是这个SO问题使我相信将它们更改为默认值可以解决问题。

The error that occurs when I change default back to application is the following

将默认值更改回应用程序时发生的错误如下

ExecJS::ProgramError in Application#home
Showing c:/Users/***/Desktop/loadtest/app/views/layouts/application.html.erb where line #5 raised:
TypeError: Object doesn't support this property or method
    Rails.root: c:/Users/aterra/Desktop/loadtest

I have not changed my application.js or application.css in any way and look how they would look when you build a rails application.

我没有以任何方式更改我的application.js或application.css,看看在构建rails应用程序时它们的外观。

If you need to see my application controller it looks like this

如果您需要查看我的应用程序控制器,它看起来像这样

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  def home   
  end
end

Any help in solving this question would be greatly appreciated. Thank you for you time.

任何帮助解决这个问题将不胜感激。谢谢你的时间。

EDIT:

I resolved my problem by changing default to application.css and application.js respectively. After that I went into my application.js folder and added =require turbolinks without the // and then voila it started working.

我通过将默认值分别更改为application.css和application.js来解决我的问题。之后我进入了我的application.js文件夹并添加了= require turbolinks而没有//然后瞧它开始工作了。

2 个解决方案

#1


0  

You can require your JS and CSS files in your application.[js|css] In your application.js, append //= require default and in your application.css, append *= require default, default being the name of your JS/CSS files respectively. You can learn more about the Assets Pipeline here: http://guides.rubyonrails.org/asset_pipeline.html

你可以在你的应用程序中要求你的JS和CSS文件。[js | css]在你的application.js中,追加// = require default,在你的application.css中,追加* = require default,默认是你的JS /的名字分别是CSS文件。您可以在此处了解有关资产管道的更多信息:http://guides.rubyonrails.org/asset_pipeline.html

#2


0  

By default Rails will look for application.js for the manifest. If you want to use default.js you have to implicitly instruct Rails to look for it:

默认情况下,Rails将为清单查找application.js.如果要使用default.js,则必须隐式指示Rails查找它:

Try adding the following to /config/initializers/assets.rb

尝试将以下内容添加到/config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( default.js )

#1


0  

You can require your JS and CSS files in your application.[js|css] In your application.js, append //= require default and in your application.css, append *= require default, default being the name of your JS/CSS files respectively. You can learn more about the Assets Pipeline here: http://guides.rubyonrails.org/asset_pipeline.html

你可以在你的应用程序中要求你的JS和CSS文件。[js | css]在你的application.js中,追加// = require default,在你的application.css中,追加* = require default,默认是你的JS /的名字分别是CSS文件。您可以在此处了解有关资产管道的更多信息:http://guides.rubyonrails.org/asset_pipeline.html

#2


0  

By default Rails will look for application.js for the manifest. If you want to use default.js you have to implicitly instruct Rails to look for it:

默认情况下,Rails将为清单查找application.js.如果要使用default.js,则必须隐式指示Rails查找它:

Try adding the following to /config/initializers/assets.rb

尝试将以下内容添加到/config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( default.js )