如何在Rails应用程序中为引擎提供图像资产

时间:2022-04-14 00:23:28

I'm having trouble getting image assets in my engine to render inside the host app.

我在引擎中无法在主机应用中渲染图像资产。

My host app is capable of grabbing css and javascript from then engine thanks to code like this in the view/layout/application.html.erb file:

由于视图/布局/应用程序.html中的代码,我的主机应用程序能够从那时起获取css和javascript。erb文件:

 <%= stylesheet_link_tag    'my_engine/application', :media => 'all' %>
 <%= javascript_include_tag 'my_engine/application' %> 

However, I don't know of a similar method for including the images from my engine. Any ideas on how I can get the engine's image files to render in the host app?

但是,我不知道一个类似的方法,包括我的引擎的图像。关于如何让引擎的图像文件在主机应用程序中呈现,有什么想法吗?

Edit:

编辑:

In my engine's engine.rb file, I've added the images directory to the app's directory like this:

在我的引擎的引擎。rb文件,我把images目录添加到app的目录如下:

initializer :assets do |config|
  Rails.application.config.assets.paths << root.join("app", "assets", "images", "my_engine", "some_subdirectory")
end

and the view has an image tag that looks like this:

视图的图像标签是这样的

<%= image_tag "/my_engine/some_subdirectory/picture.png" %>

but my server logs, I see this:

但是我的服务器日志,我看到了

Started GET "/assets/some_subdirectory/picture.png" for 127.0.0.1 at 2014-04-06 20:11:38 -0500
Served asset /some_subdirectory/picture.png - 404 Not Found (3ms)

ActionController::RoutingError (No route matches [GET] "/assets/some_subdirectory/picture.png"):

1 个解决方案

#1


2  

You don't need to do this at all because Rails does it for you:

你根本不需要这样做,因为Rails为你做:

initializer :assets do |config|
  Rails.application.config.assets.paths << root.join("app", "assets", "images", "my_engine", "some_subdirectory")
end

Instead, you can reference the images exactly the same way as you would as if they were in the application:

相反,你可以像在应用程序中那样引用这些图像:

<%= image_tag "your_image.jpg" %>

I'm assuming here that your_image.jpg is at /app/assets/images/your_image.jpg within your engine. If it's in a subdirectory, then:

我假设您的_image.jpg在/app/assets/images/your_image.jpg引擎中。如果在子目录中,则:

<%= image_tag "subdirectory/your_image.jpg" %>

There's no need to include the name of your engine in the path unless the file is at some place like app/assets/images/your_engine/your_image.jpg, in which case:

不需要在路径中包含引擎的名称,除非文件位于某个位置,如app/assets/images/your_engine/your_image.jpg,在这种情况下:

<%= image_tag "your_engine/your_image.jpg" %>

Would be the correct way to do this.

这是正确的方法。

The Engines Guide contains more useful information about engines too.

引擎指南也包含更多关于引擎的有用信息。

#1


2  

You don't need to do this at all because Rails does it for you:

你根本不需要这样做,因为Rails为你做:

initializer :assets do |config|
  Rails.application.config.assets.paths << root.join("app", "assets", "images", "my_engine", "some_subdirectory")
end

Instead, you can reference the images exactly the same way as you would as if they were in the application:

相反,你可以像在应用程序中那样引用这些图像:

<%= image_tag "your_image.jpg" %>

I'm assuming here that your_image.jpg is at /app/assets/images/your_image.jpg within your engine. If it's in a subdirectory, then:

我假设您的_image.jpg在/app/assets/images/your_image.jpg引擎中。如果在子目录中,则:

<%= image_tag "subdirectory/your_image.jpg" %>

There's no need to include the name of your engine in the path unless the file is at some place like app/assets/images/your_engine/your_image.jpg, in which case:

不需要在路径中包含引擎的名称,除非文件位于某个位置,如app/assets/images/your_engine/your_image.jpg,在这种情况下:

<%= image_tag "your_engine/your_image.jpg" %>

Would be the correct way to do this.

这是正确的方法。

The Engines Guide contains more useful information about engines too.

引擎指南也包含更多关于引擎的有用信息。