如何使用Rails 3显示路径位于Ruby on Rails项目目录之外的映像?

时间:2022-08-02 20:42:24

how to display the image, which stored outside from the project directory?

如何显示存储在项目目录之外的图像?

Is there a simple way to do it?

有简单的方法吗?

3 个解决方案

#1


9  

I see two ways :

我看到了两种方式:

  • On top of your rails server, in production and sometimes in dev, you have to use a web server like apache or nginx. With these, you can serve files from other directories for specific URL. E.G. you can make http://yourapp.com/images/ serving files from a specific dir. In rails, display the image with a traditionnal image_tag
  • 在rails服务器之上,在产品中,有时在开发中,您必须使用apache或nginx这样的web服务器。有了这些,您可以从其他目录中为特定的URL提供文件。例如,您可以从特定的目录中创建http://yourapp.com/images/服务文件。在rails中,使用传统的image_tag显示图像

Example with Nginx :

Nginx的例子:

    # Find the right `server` section which you currently use to serve your rails app
server {
      listen 80;

      # Add this
      location /images {
        root /var/www/my/specific/folder;
      }

      location / {
        #...
        #... Here you should already some code to proxy to your rails server
      }
    }

With that, when you access to `yourserver.com/images`, nginx serve your specific folder and not your rails app. Then in your app view :

    <%= image_tag 'http://yourserver.com/images/my_pic.jpg' %>
  • If you can't access your server settings, you can serve an image file from a controller action with send_file

    如果不能访问服务器设置,可以使用send_file从控制器操作中提供图像文件

    In a controller :

    控制器:

    class ImagesController < ApplicationController
      def show
        send_file File.join('/var/www/my/specific/folder',params[:name]), :disposition => 'inline'
      end
    end
    

    In config/routes.rb

    在config / routes.rb

    match '/images/:name' => 'images#show', :as => :custom_image
    

    Then when you access this action (via the route you defined in config/routes.rb), you have the image. So in your view you do a traditionnal image_tag with this URL :

    然后,当您访问此操作(通过在config/routes.rb中定义的路由)时,您就得到了映像。在你的视图中你用这个URL做一个传统的image_tag

    <%= image_tag custom_image_path( 'my_pic.jpg' ) %>
    OR
    <%= image_tag custom_image_url( 'my_pic.jpg' ) %>
    

#2


0  

If it's stored outside of the Rails app directory, then it does not belong to asset pipeline and you can simply link to it:

如果它存储在Rails应用程序目录之外,那么它不属于资产管道,您可以直接链接到它:

<%= image_tag("http://example.com/some_file.jpg") %>

Obviously it must be accessible through HTTP (you need nginx or Apache server installed).

显然,它必须通过HTTP访问(您需要安装nginx或Apache服务器)。

#3


0  

This is probably bad idea and will lead to a bunch of issues. Some security, some functionality, but most of the effects I actually don't know.
I do know, from experience, that whenever you go against the rails conventions for what and where stuff is, it's a slippery slope of things breaking, best avoided.

这可能是个坏主意,会导致很多问题。有些安全,有些功能,但大部分效果我都不知道。我知道,从经验来看,无论什么时候,当你违反了rails的约定,什么东西是什么,什么东西是,它是一个很容易被打破的滑坡,最好避免。

Create a solution using the framework provided.

使用提供的框架创建解决方案。

Note this functionality can also be affected a bit if you are using rails 3.1+ as opposed to <= 3.0 due to asset compilation which copies js, css and images into public.

注意,如果您使用的是rails 3.1+,而不是<= 3.0,那么这个功能也会受到一些影响。

#1


9  

I see two ways :

我看到了两种方式:

  • On top of your rails server, in production and sometimes in dev, you have to use a web server like apache or nginx. With these, you can serve files from other directories for specific URL. E.G. you can make http://yourapp.com/images/ serving files from a specific dir. In rails, display the image with a traditionnal image_tag
  • 在rails服务器之上,在产品中,有时在开发中,您必须使用apache或nginx这样的web服务器。有了这些,您可以从其他目录中为特定的URL提供文件。例如,您可以从特定的目录中创建http://yourapp.com/images/服务文件。在rails中,使用传统的image_tag显示图像

Example with Nginx :

Nginx的例子:

    # Find the right `server` section which you currently use to serve your rails app
server {
      listen 80;

      # Add this
      location /images {
        root /var/www/my/specific/folder;
      }

      location / {
        #...
        #... Here you should already some code to proxy to your rails server
      }
    }

With that, when you access to `yourserver.com/images`, nginx serve your specific folder and not your rails app. Then in your app view :

    <%= image_tag 'http://yourserver.com/images/my_pic.jpg' %>
  • If you can't access your server settings, you can serve an image file from a controller action with send_file

    如果不能访问服务器设置,可以使用send_file从控制器操作中提供图像文件

    In a controller :

    控制器:

    class ImagesController < ApplicationController
      def show
        send_file File.join('/var/www/my/specific/folder',params[:name]), :disposition => 'inline'
      end
    end
    

    In config/routes.rb

    在config / routes.rb

    match '/images/:name' => 'images#show', :as => :custom_image
    

    Then when you access this action (via the route you defined in config/routes.rb), you have the image. So in your view you do a traditionnal image_tag with this URL :

    然后,当您访问此操作(通过在config/routes.rb中定义的路由)时,您就得到了映像。在你的视图中你用这个URL做一个传统的image_tag

    <%= image_tag custom_image_path( 'my_pic.jpg' ) %>
    OR
    <%= image_tag custom_image_url( 'my_pic.jpg' ) %>
    

#2


0  

If it's stored outside of the Rails app directory, then it does not belong to asset pipeline and you can simply link to it:

如果它存储在Rails应用程序目录之外,那么它不属于资产管道,您可以直接链接到它:

<%= image_tag("http://example.com/some_file.jpg") %>

Obviously it must be accessible through HTTP (you need nginx or Apache server installed).

显然,它必须通过HTTP访问(您需要安装nginx或Apache服务器)。

#3


0  

This is probably bad idea and will lead to a bunch of issues. Some security, some functionality, but most of the effects I actually don't know.
I do know, from experience, that whenever you go against the rails conventions for what and where stuff is, it's a slippery slope of things breaking, best avoided.

这可能是个坏主意,会导致很多问题。有些安全,有些功能,但大部分效果我都不知道。我知道,从经验来看,无论什么时候,当你违反了rails的约定,什么东西是什么,什么东西是,它是一个很容易被打破的滑坡,最好避免。

Create a solution using the framework provided.

使用提供的框架创建解决方案。

Note this functionality can also be affected a bit if you are using rails 3.1+ as opposed to <= 3.0 due to asset compilation which copies js, css and images into public.

注意,如果您使用的是rails 3.1+,而不是<= 3.0,那么这个功能也会受到一些影响。