Rails没有在app/assets/images文件夹中预编译图像吗?

时间:2022-05-16 00:27:10

I have some images (svg) in my app/assets/images folder. According to the Rails Guides, all the files in the assets folder should be automatically precompiled.

我在app/assets/images文件夹里有一些图片(svg)。根据Rails指南,应该自动预编译assets文件夹中的所有文件。

However, when I reference the the image using image_tag('filename'), it shows me an Sprockets::Rails::Helper::AssetNotPrecompiled error

但是,当我使用image_tag('filename')引用图像时,它会显示一个spro佝偻::Rails::Helper:: assetnotprecompile error

Asset was not declared to be precompiled in production.

资产在生产中未被声明为预编译。

It tells me to declare the file to be precompiled manually, but why should that be necessary? On top of that, why does it concern itself with the production environment when I am doing everything in development?

它告诉我要手动声明要预编译的文件,但是为什么需要这样做呢?最重要的是,当我在开发过程中处理所有事情时,为什么还要关注生产环境呢?

3 个解决方案

#1


4  

It's probably because you didn't specify the complete image name. I ran into this problem after updating the gem too. Before I just used image_tag 'some-image', but it seems that you now have to specify what type of image/extension you want to use.

这可能是因为您没有指定完整的映像名。我也在更新gem之后遇到了这个问题。在我使用image_tag 'some-image'之前,您似乎必须指定要使用的图像/扩展类型。

Try this: image_tag 'some-image.svg'. It worked for me.

试试这个:image_tag“some-image.svg”。它为我工作。

Cheers.

欢呼。

#2


19  

If you added the image after you've started the server in development, restart the server. Sprockets will then precompile that image and the error will go away.

如果在开发中启动服务器后添加映像,则重新启动服务器。然后,spro佝偻病会预编译那个图像,错误就会消失。

#3


6  

I'm pretty sure Rails doesn't support .svg yet, hence why it would ignore it.

我确信Rails还不支持.svg,因此它会忽略它。

You'll need to include the file extensions in your config/application.rb file:

您需要在配置/应用程序中包含文件扩展名。rb文件:

#config/application.rb
config.assets.precompile += %w(.svg)

In regards the application concerning itself with the production environment, you have to remember that the precompilation process is meant for production:

关于与生产环境有关的应用程序,您必须记住,预编译过程是用于生产的:

The first feature of the pipeline is to concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.

管道的第一个特性是连接资产,它可以减少浏览器用于呈现web页面的请求数量。Web浏览器可以同时发出的请求数量有限,因此请求越少,应用程序的加载速度就越快。

Concantenating assets essentially means to compile your asset files into a single file, which is typically then minified.

托管资产本质上意味着将您的资产文件编译成一个单独的文件,然后通常将其缩小。

--

- - -

Although this can be done in real-time, it's mostly the realm of static assets (which have to be precompiled). This means that if you run the rake asstes:precompile task, it will work on the development environment, unless you call RAILS_ENV=production rake assets:precompile (which sets it to the production environment for that request.

虽然这可以实时完成,但这主要是静态资产的领域(必须预先编译)。这意味着,如果您运行rake asstes:precompile任务,它将在开发环境中工作,除非您调用RAILS_ENV=production rake资产:precompile(它将其设置为该请求的生产环境)。

why does it concern itself with the production environment when I am doing everything in development

当我在开发过程中做所有的事情时,为什么还要关注生产环境呢

The application is going to run in production, not development.

应用程序将在生产环境中运行,而不是在开发环境中运行。

Ultimately, everything you do in development should make it easier / better to work in production. In the sense of your assets, it means that you can use many of the quirks of Rails' asset pipeline, from sprockets to preprocessors such as SASS & Coffeescript

最终,您在开发中所做的每一件事都应该使在生产中工作变得更容易/更好。就您的资产而言,这意味着您可以使用Rails的资产管道的许多怪癖,从spro佝偻病到SASS & coffecript等预处理程序

#1


4  

It's probably because you didn't specify the complete image name. I ran into this problem after updating the gem too. Before I just used image_tag 'some-image', but it seems that you now have to specify what type of image/extension you want to use.

这可能是因为您没有指定完整的映像名。我也在更新gem之后遇到了这个问题。在我使用image_tag 'some-image'之前,您似乎必须指定要使用的图像/扩展类型。

Try this: image_tag 'some-image.svg'. It worked for me.

试试这个:image_tag“some-image.svg”。它为我工作。

Cheers.

欢呼。

#2


19  

If you added the image after you've started the server in development, restart the server. Sprockets will then precompile that image and the error will go away.

如果在开发中启动服务器后添加映像,则重新启动服务器。然后,spro佝偻病会预编译那个图像,错误就会消失。

#3


6  

I'm pretty sure Rails doesn't support .svg yet, hence why it would ignore it.

我确信Rails还不支持.svg,因此它会忽略它。

You'll need to include the file extensions in your config/application.rb file:

您需要在配置/应用程序中包含文件扩展名。rb文件:

#config/application.rb
config.assets.precompile += %w(.svg)

In regards the application concerning itself with the production environment, you have to remember that the precompilation process is meant for production:

关于与生产环境有关的应用程序,您必须记住,预编译过程是用于生产的:

The first feature of the pipeline is to concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.

管道的第一个特性是连接资产,它可以减少浏览器用于呈现web页面的请求数量。Web浏览器可以同时发出的请求数量有限,因此请求越少,应用程序的加载速度就越快。

Concantenating assets essentially means to compile your asset files into a single file, which is typically then minified.

托管资产本质上意味着将您的资产文件编译成一个单独的文件,然后通常将其缩小。

--

- - -

Although this can be done in real-time, it's mostly the realm of static assets (which have to be precompiled). This means that if you run the rake asstes:precompile task, it will work on the development environment, unless you call RAILS_ENV=production rake assets:precompile (which sets it to the production environment for that request.

虽然这可以实时完成,但这主要是静态资产的领域(必须预先编译)。这意味着,如果您运行rake asstes:precompile任务,它将在开发环境中工作,除非您调用RAILS_ENV=production rake资产:precompile(它将其设置为该请求的生产环境)。

why does it concern itself with the production environment when I am doing everything in development

当我在开发过程中做所有的事情时,为什么还要关注生产环境呢

The application is going to run in production, not development.

应用程序将在生产环境中运行,而不是在开发环境中运行。

Ultimately, everything you do in development should make it easier / better to work in production. In the sense of your assets, it means that you can use many of the quirks of Rails' asset pipeline, from sprockets to preprocessors such as SASS & Coffeescript

最终,您在开发中所做的每一件事都应该使在生产中工作变得更容易/更好。就您的资产而言,这意味着您可以使用Rails的资产管道的许多怪癖,从spro佝偻病到SASS & coffecript等预处理程序