如何配置 Rails 来访问不在其常规目录位置的媒体资源?
假设我有一个图像不在正常位置:
{appname}/public/images/unconventional.gif
但是在这里:
{appname}/unconventional.gif
我知道这完全违反了 Rails 约定,是不道德的,在任何情况下都不应该这样做,而且,为什么我什至会建议有这么蠢的事吗?
好的,现在我们已经解决了这个问题,假设我使用的是 Windows,因此符号链接是不可能的,那么如何进行设置呢?
Let's say I have an image that does not reside in the normal location:
{appname}/public/images/unconventional.gif
But instead here:
{appname}/unconventional.gif
I understand this is a complete violation of Rails conventions, is immoral and you should never do this under any circumstances and, furthermore, why would I even suggest such a foolish thing?
Ok, now that we have that out of the way, assuming I am on Windows and therefore symbolic links are out of the question, how is it possible to set this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 不提供这些图像,它让 Web 服务器提供这些图像。 您最好更改 Web 服务器的配置来处理这种情况。 例如,如果您使用 Apache,则使用 mod_rewrite 进行设置会相当容易。
让 Rails 提供这些图像会很丑陋,但是如果您在
routes.rb
中提供与/public/images/unconventional.gif 匹配的路线,这是可能的
,并且如果文件本身不存在。 例如:然后创建一个控制器
StaticImageController
:警告: 如果您使用上述概念,请注意,如果您使用 URL 中的输入来决定提供哪个文件(使用例如,
params[:file]
),您需要彻底清理输入,因为您面临着将整个文件系统暴露给外界的风险。Rails does not serve these images, it lets the web server do that. You had best change the configuration of your web server to handle this scenario. If you use
Apache
, for example, it would fairly easy to set up withmod_rewrite
.Making Rails serve these images will be ugly, but it is possible if you provide a route in your
routes.rb
that matches/public/images/unconventional.gif
, and if the file itself does not exist. For example:And then create a controller
StaticImageController
:Warning: if you use the above concept, note that if you use input from the URL to decide which file to serve (with
params[:file]
, for example), you need to thoroughly sanitize the input, because you are risking exposing your entire file system to the outside world.