Ruby on rails 3.2 如何配置';高电压';gem提供静态图像吗?

Ruby on rails 3.2 如何配置';高电压';gem提供静态图像吗?,ruby-on-rails-3.2,high-voltage,Ruby On Rails 3.2,High Voltage,我正试图使用“高压”gem来确保我的页面周围的身份验证(实际上,一些完全独立的站点有一个静态html/css树)。这可以工作,但不加载图像。如果导航到图像的路径,图像的源代码将作为文本返回,从而导致“ActionView::ErrorEncodingErrorinPages#show”。如果我将静态html文件夹放在/public中,所有图像都会按预期加载。显然,我的PageController试图像模板一样呈现静态图像。我怎样才能解决这个问题 我已按如下所述覆盖了PageController:

我正试图使用“高压”gem来确保我的页面周围的身份验证(实际上,一些完全独立的站点有一个静态html/css树)。这可以工作,但不加载图像。如果导航到图像的路径,图像的源代码将作为文本返回,从而导致“ActionView::ErrorEncodingErrorinPages#show”。如果我将静态html文件夹放在/public中,所有图像都会按预期加载。显然,我的PageController试图像模板一样呈现静态图像。我怎样才能解决这个问题

我已按如下所述覆盖了PageController:


#在config/routes.rb中
匹配“/pages/*id”=>“pages#show”,:as=>:page,:format=>false
#应用程序内/controller/pages\u controller.rb
在\u筛选器之前:验证\u客户端!
布局:无
def秀
自定义身份验证
超级的
结束
...
我是一个RoR新手,所以欢迎任何一般性的帮助或建议

编辑:这是完整的错误消息


ActionView::页面中的错误编码错误#显示

显示/home/robert/Coding/RailsPlayground/JoSchaefer/app/views/pages/myWebsite/images/test.png,其中行#凸起: 您的模板未保存为有效的UTF-8。请在文本编辑器中指定UTF-8作为模板的编码,或通过插入以下内容作为模板的第一行来标记模板及其编码: #编码:。 模板的源是: �巴布亚新几内亚
IHDRKK�,��文本软件Adobe ImageReadyq�e如果您使用的是Rails 3.2,那么您可能依赖于资产管道。惯例是将图像放置在资产目录中,这些目录(默认情况下):
app/assets/images
lib/assets/images
vendor/assets/images
。可能的解决方案是将图像移动到该位置或扩展资产目录,如中所述

编辑:扩展案例中的资产目录(添加到
应用程序.rb
):


config.assets.path在我查找“高压”gem的源代码后,我能够通过内联发送原始文件来“解决”我的问题:


#在/app/controllers/pages\u controller.rb中
def秀
logger.debug“当前页面为:#{Current_page}”
自定义\u身份验证当前\u页面
发送_文件“app/views/#{current_page}”,:disposition=>“inline”
#超级
结束
这是被跳过的高压::页面控制器的超级方法:


#“高压”gem的来源
#应用程序内/控制器/高压/pages_controller.rb
def秀
呈现:模板=>当前页面
结束
我知道,因为
当前页面
是从
参数[:id]
派生的。但是,
HighVoltage::PageFinder
显然会清除给定的输入:


#“高压”gem的来源
#在lib/high_voltage/page_finder.rb中
有效的字符数=“a-zA-Z0-9~!@$%^&*()”````+-=\“{}}124;[];”,?“.别动
...
def清洁路径
path=Pathname.new(“/#{clean_id}”)
path.cleanpath.to_s[1..-1]
结束
def清洁标识
@page_id.tr(“^#{VALID_CHARACTERS},”)
结束

导航到
http://localhost:3000/pages/../shouldNotBeAccessed.html
导致
http://localhost:3000/shouldNotBeAccessed.html
并且从不调用
PagesController
,这是正常的,
http://localhost:3000/pages/something/../somethingElse.html
调用
页面控制器和日志ger给我的“当前页面是:pages/somethingElse.html,因此路径遍历是可能的,只要它发生在app/views/pages/下。。这对我来说是可以接受的。

但是
错误的编码听起来很奇怪,图像的格式是什么?它们放在哪里了?app/views/pages/myWebsite/images谢谢你的回答,编辑了我的问题(Rails版本3.2.11)。因此,没有办法保持目录结构不变?有一种方法,将图像位置添加到
应用程序.rb
文件中的资产目录中。很抱歉,我仍然面临“错误编码错误”,所有乱七八糟的图像数据都呈现为下面的文本。我必须在/a/文件夹中的任何地方“//=需要任何东西吗?你能发布准确的错误消息吗?编码与字符集和字符串数据有关,而与图像无关……确切地说,这就是为什么我认为控制器错误地将图像呈现为文本的原因之一。当然,如果有更合适的方法使用“高压”来实现这一点,我会很高兴。此方法将显示图像,但不会呈现非图像文件的布局。我检查请求是否为图像,如果是,则使用此代码,如果不是,则调用super<代码>如果当前页面包含?“.png”send_file“app/views/#{current_page}”,:disposition=>“inline”else super end
ActionView::WrongEncodingError in Pages#show

Showing /home/robert/Coding/RailsPlayground/JoSchaefer/app/views/pages/myWebsite/images/test.png where line # raised:

Your template was not saved as valid UTF-8. Please either specify UTF-8 as the encoding for your template in your text editor, or mark the template with its encoding by inserting the following as the first line of the template:

# encoding: <name of correct encoding>.

The source of your template was:

�PNG


IHDRKK�,��tEXtSoftwareAdobe ImageReadyq�e<"iTXtXML:com.adobe.xmp<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.0-c060 61.134777, 2010/02/12-17:32:00        "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmp:CreatorTool="Adobe Photoshop CS5 Macintosh" xmpMM:InstanceID="xmp.iid:AF83234B3E0211E281788B850106876A" xmpMM:DocumentID="xmp.did:AF83234C3E0211E281788B850106876A"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:AF8323493E0211E281788B850106876A" stRef:documentID="xmp.did:AF83234A3E0211E281788B850106876A"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>|~�
kIDATx��[
TTe�;�p���?��:Y�����$Obb-m֖���V��B{2�S�g�
O[[�G����)�z<�4ꒉ��16��0���s��v�3#0nr�Μ9Ýo�w��}��}��^��N�dL�X���#G8V�x�e�Ǐ=��ɱ���`������?�64K�T��hl"���a�Z"�dU5�/5�A��{wkb���غ�C_/�w��w�,�>�_&��۝�'CN2�)�]��V��g�In case you are using Rails 3.2 , you are probably relying on asset-pipeline. The convention is to place images in the asset directories , which are (by default): 
app/assets/images
,
lib/assets/images
and
vendor/assets/images
. The possible solutions are to move your images there or to extend asset directories as described in this Rails guide.

EDIT : extending assets directories in your case (add in your
application.rb
):

config.assets.paths << Rails.root.join("app","views","pages","myWebsite")