简体   繁体   English

如何在Ruby on Rails应用程序中使用Javascript获取CSS文件的内容?

[英]How to fetch content of CSS file with Javascript in a Ruby on Rails application?

I need to fetch a CSS file's content with JavaScript in a Ruby on Rails application, but it seems that every request I make tries to make a request to a page rather than fetch a specific file. 我需要在Ruby on Rails应用程序中使用JavaScript提取CSS文件的内容,但是似乎我提出的每个请求都试图向页面发出请求,而不是获取特定文件。

I'm not sure how an AJAX request works when working with Ruby on Rails, and my searches doesn't really answer this question either. 我不确定在使用Ruby on Rails时AJAX请求如何工作,我的搜索也没有真正回答这个问题。

This is how I make the request (using jQuery's $.ajax ): 这就是我发出请求的方式(使用jQuery的$.ajax ):

$.ajax({
    url: './assets/stylesheets/application.css.scss.erb',
    success: function(data) {
        console.log(data);
    }
});

But this just spits out this error: 但这只会吐出这个错误:

GET http://localhost:3000/assets/stylesheets/application.css.scss.erb 404 (Not Found)

This makes sense since I don't have a route of that kind, but what do I need to add in order to get a specific file's content rather than a route's? 这是有道理的,因为我没有这种路由,但是为了获得特定文件的内容而不是路由的内容,我需要添加什么呢?

You may want to learn how the asset pipeline work, I suggest reading Asset Pipeline . 您可能想学习资产管道的工作方式,建议阅读Asset Assetline

But in your case the css file will be at: 但是在您的情况下,css文件位于:

http://localhost:3000/assets/application.css

And that file includes all the css precomplied in that file, meaning if your application.css file is something like : 该文件包括该文件中预完成的所有css,这意味着您的application.css文件是否类似于:

/*
 *= require datepicker
 *= require responsive
 *= require themes
 *= require_self
 */

It means the application css file will contains all of those files content within it. 这意味着应用程序css文件将在其中包含所有这些文件的内容。

Anyway, regarding your question- so you can try: 无论如何,关于您的问题-因此您可以尝试:

$.ajax({
    url: './assets/application.css',
    success: function(data) {
        console.log(data);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM