Ruby-on-rails Rails 3:如何在控制器中获取图像路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6519141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:29:23  来源:igfitidea点击:

Rails 3: How to get image path in Controller?

ruby-on-railsruby-on-rails-3

提问by Misha Moroshko

To get the image path in Controller I use the following method:

要在控制器中获取图像路径,我使用以下方法:

class AssetsController < ApplicationController
  def download(image_file_name)
    path = Rails.root.join("public", "images", image_file_name).to_s
    send_file(path, ...)
  end
end

Is there a better method to find the path ?

有没有更好的方法来找到路径?

回答by deefour

ActionController::Base.helpers.asset_path('missing_file.jpg')

Access Asset Path from Rails Controller

从 Rails 控制器访问资产路径

回答by Matt Huggins

Not sure if this was added as early as Rails 3, but is definitely working in Rails 3.1. You can now access the view_contextfrom your controller, which allows you to call methods that are normally accessible to your views:

不确定这是否早在 Rails 3 中添加,但肯定在 Rails 3.1 中工作。您现在可以view_context从控制器访问,这允许您调用通常可以访问您的视图的方法:

class AssetsController < ApplicationController
  def download(image_file_name)
    path = view_context.image_path(image_file_name)
    # ... use path here ...
  end
end

Note that this will give you the publicly accessible path (e.g.: "/assets/foobar.gif"), not the local filesystem path.

请注意,这将为您提供可公开访问的路径(例如:“/assets/foobar.gif”),而不是本地文件系统路径。

回答by Ricardo Rivas

view_context.image_path('noimage.jpg')

view_context.image_path('noimage.jpg')

回答by ilgam

Asset url:

资产网址:

ActionController::Base.helpers.asset_path(my_path)

Image url:

图片网址:

ActionController::Base.helpers.image_path(my_path)

回答by Devin

view_contextworks for me in Rails 4.2 and Rails 5.

view_context在 Rails 4.2 和 Rails 5 中对我有用。

Find some codes in Rails repo explains view_context:

在 Rails repo 中找到一些代码解释view_context

# definition
module ActionView
  # ...
  module Rendering
    # ...
    def view_context
      view_context_class.new(view_renderer, view_assigns, self)
    end
  end
end

# called in controller module
module ActionController
  # ...
  module Helpers
    # Provides a proxy to access helper methods from outside the view.
    def helpers
      @_helper_proxy ||= view_context
    end
  end
end

回答by Marius Butuc

You might want to check out image_path(image.png)for your scenario.

您可能想要查看image_path(image.png)您的场景。

Here are the examples from the docs:

以下是文档中的示例:

image_path("edit")                                 # => "/images/edit"
image_path("edit.png")                             # => "/images/edit.png"
image_path("icons/edit.png")                       # => "/images/icons/edit.png"
image_path("/icons/edit.png")                      # => "/icons/edit.png"
image_path("http://www.example.com/img/edit.png")  # => "http://www.example.com/img/edit.png"