passenger使ってて
image_tagで指定した画像は表示できるがcssで指定した画像が表示できない。
ってことがあって、ちょっとパニック。
(script/serverでの起動だと問題なく表示できる)
とりあえずimage_tagってどうなってるんだと思って
ソースをチラ見
cd /opt/local/lib/ruby/gems/1.8/gems/
view actionpack-2.3.5/lib/action_view/helpers/asset_tag_helper.rb
def image_tag(source, options = {}) options.symbolize_keys! options[:src] = path_to_image(source) options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize if size = options.delete(:size) options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} end if mouseover = options.delete(:mouseover) options[:onmouseover] = "this.src='#{image_path(mouseover)}'" options[:onmouseout] = "this.src='#{image_path(options[:src])}'" end tag("img", options) end
path_to_image
def image_path(source) compute_public_path(source, 'images') end alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
compute_public_path
あったよこれか。"#{ActionController::Base.relative_url_root}#{source}"
ふむふむ。だから、environment.rbとかに↓みたいに書いてたりしたのか。(今は不要)
config.action_controller.relative_url_root = "/myapp"
def compute_public_path(source, dir, ext = nil, include_host = true) has_request = @controller.respond_to?(:request) source_ext = File.extname(source)[1..-1] if ext && (source_ext.blank? || (ext != source_ext && File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}")))) source += ".#{ext}" end unless source =~ %r{^[-a-z]+://} source = "/#{dir}/#{source}" unless source[0] == ?/ source = rewrite_asset_path(source) if has_request && include_host unless source =~ %r{^#{ActionController::Base.relative_url_root}/} source = "#{ActionController::Base.relative_url_root}#{source}" end end end if include_host && source !~ %r{^[-a-z]+://} host = compute_asset_host(source) if has_request && !host.blank? && host !~ %r{^[-a-z]+://} host = "#{@controller.request.protocol}#{host}" end "#{host}#{source}" else source end end
ちなみにurlの後ろのidはココでごにょごにょ
ファイルのmtime(更新日時)を取得してto_iしてる。
この手法はいいですね。
cssで定義した画像もこういう風にできないかな。
def rewrite_asset_path(source) asset_id = rails_asset_id(source) if asset_id.blank? source else source + "?#{asset_id}" end def rails_asset_id(source) if asset_id = ENV["RAILS_ASSET_ID"] asset_id else if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source]) asset_id else path = File.join(ASSETS_DIR, source) asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : '' if @@cache_asset_timestamps @@asset_timestamps_cache_guard.synchronize do @@asset_timestamps_cache[source] = asset_id end end asset_id end end end
本題
rails/passengerの動きは特にまずいことなさそうなので、
cssでの画像パス指定をどうするかってとこに行き着いた。
で、ココ。
CSS background image help (Page 1) - XHTML/CSS/JavaScript - Rails Forum - Ruby on Rails Help and Discussion Forum
ありがとうFreddy。
cssからの相対パス指定にすればokでした。
よくよく考えると全然大した話じゃないけど、
railsやpassengerが何かやってんじゃないかと勘繰って嵌ってしまった。
あースッキリ。
変更前
background-image:url(/images/hoge.gif)
変更後
background-image:url(../images/hoge.gif)
追加
users guideに書いてた。
Phusion Passenger users guideメモ2 - うんたらかんたらRuby - Rubyist