[Rails] How to Get View Image URLs from Within a Controller
In Ruby on Rails, when you want to get view image_path etc. from within a controller, you can use view_context.
■ Controller Example
#-*- encoding: utf-8 -*-
class BookController < ApplicationController
def show
view_context.image_path('book.jpg')
# => "http://localhost:3000/images/book.jpg"
end
end
■ application.rb
module MyApp
class Application < Rails::Application
config.action_controller.asset_host = "http://localhost:3000"
...(omitted)...
end
end
That’s all.
【Reference】
・Rails のコントローラの中で View の画像URLを取得する方法 - それはBlog (How to Get View Image URLs from Within a Rails Controller - That’s Blog)
That’s all from the Gemba.