LoginSignup
6
3

More than 5 years have passed since last update.

carrierwaveが出力するパスにホスト名を含める

Last updated at Posted at 2016-05-05

carrierwaveが標準で出力するパスはこんな感じなのですが

Post.image.url  #=> /uploads/post/image...

APIなどを作っているとドメインまで含めたパスを出力したくなります。

そういうときは
/config/initializers/にcarrierwave.rbを作って

CarrierWave.configure do |config|
  config.asset_host = "http://localhost:3000"
end

と書いておけばOK

ちなみに今回はURLを定数にしておきたかったので

/config/initializers/に constants.rbを作って


module Constants
  if Rails.env == "production"
    URL = "https://production.com/"
  else
    URL = "http://localhost:3000/"
  end
end

こうしてみました。

require_relative "constants"
CarrierWave.configure do |config|
  config.asset_host = Constants::URL
end
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3