Rails 3.1提供来自供应商/资产/图像的图像

时间:2021-01-20 20:41:43

I am trying to put some external images (used by a jQuery plugin) to vendor/assets/images in my Rails 3.1 app. Problem is that when I try something like:

我试图在我的Rails 3.1应用程序中将一些外部图像(由jQuery插件使用)放到供应商/资产/图像中。问题是,当我尝试类似的东西:

<%= image_tag "ui-bg_flat_75_ffffff_40x100.png" %>

I get an error:

我收到一个错误:

No route matches [GET] "/assets/ui-bg_flat_75_ffffff_40x100.png"

I checked my Rails.application.config.assets.paths and it list these dirs:

我检查了我的Rails.application.config.assets.paths并列出了这些目录:

..../app/assets/images
..../app/assets/javascripts
..../app/assets/stylesheets
..../vendor/assets/images
..../vendor/assets/stylesheets
..../.rvm/gems/ruby-1.9.2-p180@mygems/gems/jquery-rails-1.0.9/vendor/assets/javascripts

As you can see /vendor/assets/images is listed there. If I put my image to app/assets/images everything works.

如您所见,/ vendor / assets / images列在那里。如果我把我的图像放到app / assets / images,一切正常。

I thought that new asset pipeline was supposed to go through all assets dirs and serve requested file wherever it finds it.

我认为新的资产管道应该遍历所有资产目录并在任何地方提供所请求的文件。

Does anyone knows what's the problem here?

有谁知道这里的问题是什么?

4 个解决方案

#1


80  

I had to restart my rails server after creating the vendor/assets/images directory. Before this, I was seeing the same error as you ("No route matches [GET]").

创建vendor / assets / images目录后,我不得不重新启动rails服务器。在此之前,我看到了与你相同的错误(“没有路线匹配[GET]”)。

My guess is that the rails server does not check these directories if they did not exist when it was first started. When you open a rails console to diagnose the issue, you get a new instance of rails which knows about the directory, which only adds to the confusion.

我的猜测是,如果这些目录在首次启动时不存在,则它不会检查这些目录。当您打开rails控制台来诊断问题时,您将获得一个知道目录的新的rails实例,这只会增加混乱。

#2


22  

If you are using a jQuery UI Theme Roller theme then the problem might be that in the jquery-ui css file the images are referenced within a sub folder 'images'.

如果您使用的是jQuery UI Theme Roller主题,那么问题可能是在jquery-ui css文件中,图像在子文件夹“images”中被引用。

I.e. you either have to put your images in a folder './app/assets/images/images' or you have to edit the jquery-ui css file and remove the 'images/' folder prefix.

即您必须将图像放在'./app/assets/images/images'文件夹中,或者您必须编辑jquery-ui css文件并删除'images /'文件夹前缀。

#3


9  

The asset pipeline is described in this rails guide by Ryan Bigg (draft status at the moment).

Ryan Bigg在此导轨指南中描述了资产管道(目前的草案状态)。

http://ryanbigg.com/guides/asset_pipeline.html and http://ryanbigg.com/2011/06/sprocket-asset-tags-internals/ for the references.

http://ryanbigg.com/guides/asset_pipeline.html和http://ryanbigg.com/2011/06/sprocket-asset-tags-internals/参考。

According to this, your example should work.

根据这个,你的例子应该工作。

Extract:

提取:

Assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

资产可以放置在三个位置之一的应用程序内:app / assets,lib / assets或vendor / assets。

app/assets is for assets that are owned by the application, such as custom images, javascript files or stylesheets.

app / assets适用于应用程序拥有的资产,例如自定义图像,javascript文件或样式表。

lib/assets is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.

lib / assets适用于您自己的库代码,这些代码并不真正适合应用程序的范围或跨应用程序共享的库。

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins.

vendor / assets适用于外部实体拥有的资产,例如JavaScript插件的代码。

Any subdirectory that exists within these three locations will be added to the search path for Sprockets (visible by calling Rails.application.config.assets.paths in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it’s processed by Sprockets and then served up.

这三个位置中存在的任何子目录都将添加到Sprockets的搜索路径中(通过在控制台中调用Rails.application.config.assets.paths可见)。请求资产时,将查看这些路径以查看它们是否包含与指定名称匹配的资产。找到资产后,它将由Sprockets处理,然后提供。

I have tested with an example in my app and the same syntax as yours works. Maybe you have a typo in the name of your asset.

我已经在我的应用程序中测试了一个示例,并且与您的工作语法相同。也许你的资产名称有拼写错误。

For Martin: search path for Sprockets is visible by calling Rails.application.config.assets.paths in a console.

对于Martin:通过在控制台中调用Rails.application.config.assets.paths可以看到Sprockets的搜索路径。

#4


2  

Maybe you should create another folder in /assets/images. You make a name 'images' and then you just copy all jquery-ui image and paste on folder 'images' that you create before. Hopefully this will help you.

也许你应该在/ assets / images中创建另一个文件夹。你创建一个名称'images',然后你只需复制所有jquery-ui图像并粘贴你之前创建的文件夹'images'。希望这会对你有所帮助。

#1


80  

I had to restart my rails server after creating the vendor/assets/images directory. Before this, I was seeing the same error as you ("No route matches [GET]").

创建vendor / assets / images目录后,我不得不重新启动rails服务器。在此之前,我看到了与你相同的错误(“没有路线匹配[GET]”)。

My guess is that the rails server does not check these directories if they did not exist when it was first started. When you open a rails console to diagnose the issue, you get a new instance of rails which knows about the directory, which only adds to the confusion.

我的猜测是,如果这些目录在首次启动时不存在,则它不会检查这些目录。当您打开rails控制台来诊断问题时,您将获得一个知道目录的新的rails实例,这只会增加混乱。

#2


22  

If you are using a jQuery UI Theme Roller theme then the problem might be that in the jquery-ui css file the images are referenced within a sub folder 'images'.

如果您使用的是jQuery UI Theme Roller主题,那么问题可能是在jquery-ui css文件中,图像在子文件夹“images”中被引用。

I.e. you either have to put your images in a folder './app/assets/images/images' or you have to edit the jquery-ui css file and remove the 'images/' folder prefix.

即您必须将图像放在'./app/assets/images/images'文件夹中,或者您必须编辑jquery-ui css文件并删除'images /'文件夹前缀。

#3


9  

The asset pipeline is described in this rails guide by Ryan Bigg (draft status at the moment).

Ryan Bigg在此导轨指南中描述了资产管道(目前的草案状态)。

http://ryanbigg.com/guides/asset_pipeline.html and http://ryanbigg.com/2011/06/sprocket-asset-tags-internals/ for the references.

http://ryanbigg.com/guides/asset_pipeline.html和http://ryanbigg.com/2011/06/sprocket-asset-tags-internals/参考。

According to this, your example should work.

根据这个,你的例子应该工作。

Extract:

提取:

Assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

资产可以放置在三个位置之一的应用程序内:app / assets,lib / assets或vendor / assets。

app/assets is for assets that are owned by the application, such as custom images, javascript files or stylesheets.

app / assets适用于应用程序拥有的资产,例如自定义图像,javascript文件或样式表。

lib/assets is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.

lib / assets适用于您自己的库代码,这些代码并不真正适合应用程序的范围或跨应用程序共享的库。

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins.

vendor / assets适用于外部实体拥有的资产,例如JavaScript插件的代码。

Any subdirectory that exists within these three locations will be added to the search path for Sprockets (visible by calling Rails.application.config.assets.paths in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it’s processed by Sprockets and then served up.

这三个位置中存在的任何子目录都将添加到Sprockets的搜索路径中(通过在控制台中调用Rails.application.config.assets.paths可见)。请求资产时,将查看这些路径以查看它们是否包含与指定名称匹配的资产。找到资产后,它将由Sprockets处理,然后提供。

I have tested with an example in my app and the same syntax as yours works. Maybe you have a typo in the name of your asset.

我已经在我的应用程序中测试了一个示例,并且与您的工作语法相同。也许你的资产名称有拼写错误。

For Martin: search path for Sprockets is visible by calling Rails.application.config.assets.paths in a console.

对于Martin:通过在控制台中调用Rails.application.config.assets.paths可以看到Sprockets的搜索路径。

#4


2  

Maybe you should create another folder in /assets/images. You make a name 'images' and then you just copy all jquery-ui image and paste on folder 'images' that you create before. Hopefully this will help you.

也许你应该在/ assets / images中创建另一个文件夹。你创建一个名称'images',然后你只需复制所有jquery-ui图像并粘贴你之前创建的文件夹'images'。希望这会对你有所帮助。