如何访问我的Rails应用程序中gem生成的文件(png文件)? [英] How to access a file(png file) generated by a gem in my rails application?
问题描述
我正在编写一个gem和使用该gem的Rails应用程序.
Hi I am writing a gem and a rails application that uses this gem.
基于传递的值的gem会生成一个图像文件.(我正在将其写入gem的根目录中)
The gem based on values passed, produces a image file.(i am writing it in gem's root directory )
在我的Rails应用程序中,我想访问该图像文件.我怎样才能做到这一点.
In my rails application, I want to access that image-file. how can I do that.
我应该在gem和rails-app中进行哪些修改以实现此目标?
What modifications I should make both in the gem and the rails-app to achieve this?
推荐答案
来自铁路指南:
7将资产添加到您的宝石中
资产也可以以宝石的形式来自外部来源.
7 Adding Assets to Your Gems
Assets can also come from external sources in the form of gems.
一个很好的例子是 jquery-rails 宝石,它是Rails附带的标准的JavaScript库gem.此gem包含一个从 Rails :: Engine
继承的引擎类.这样,Rails会获悉此gem的目录可能包含资产以及 app/assets
, lib/assets
和 vendor/assets
该引擎的目录将添加到Sprockets的搜索路径中.
A good example of this is the jquery-rails gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from Rails::Engine
. By doing this, Rails is informed that the directory for this gem may contain assets and the app/assets
, lib/assets
and vendor/assets
directories of this engine are added to the search path of Sprockets.
因此,基本上,创建 vendor/assets/your-image.jpg
,然后您需要以下内容:
So basically, create vendor/assets/your-image.jpg
then you want something like:
lib/jquery/rails/engine.rb
module Jquery
module Rails
class Engine < ::Rails::Engine
end
end
end
在您看来,请使用资产助手,例如:
In your views, use the asset helpers such as:
<%= image_tag 'your-image.jpg' %>
这篇关于如何访问我的Rails应用程序中gem生成的文件(png文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!