Чтобы получить путь к изображению в контроллере, я использую следующий метод:
class AssetsController < ApplicationController
def download(image_file_name)
path = Rails.root.join("public", "images", image_file_name).to_s
send_file(path, ...)
end
end
Есть ли лучший способ найти путь?
ActionController::Base.helpers.asset_path('missing_file.jpg')
view_context
работает для меня в Rails 4.2 и Rails 5.
Найти некоторые коды в Rails объясняет репо 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