2010-06-16 46 views
1

我有一個Flash對象我想加載,我相信存儲該資產的最佳位置是在public目錄中。假設它存儲在public/flash中,那麼必須有一個更好的路徑才能到達swf,而不是我下面所做的。注意'data'元素,它有一個相對路徑。訪問Ruby on Rails'公共'目錄沒有相對路徑

def create_vnc_object 
    haml_tag :object, 
    :id => 'flash', 
    :width => '100%', 
    :height => '100%', 
    :type => 'application/x-shockwave-flash', 
    :data => '../../flash/flash.swf' do 
     haml_tag :param, 
     :name => 'movie', 
     :value => '../../flash/flash.swf' 
    end 
end 

是否有一些導軌變量指向public

回答

2

這不工作?

def create_vnc_object 
    haml_tag :object, 
    :id => 'flash', 
    :width => '100%', 
    :height => '100%', 
    :type => 'application/x-shockwave-flash', 
    :data => '/flash/flash.swf' do 
     haml_tag :param, 
     :name => 'movie', 
     :value => '/flash/flash.swf' 
    end 
end 

另外,您可以使用root_url作爲前綴:

def create_vnc_object 
    haml_tag :object, 
    :id => 'flash', 
    :width => '100%', 
    :height => '100%', 
    :type => 'application/x-shockwave-flash', 
    :data => root_url + 'flash/flash.swf' do 
     haml_tag :param, 
     :name => 'movie', 
     :value => root_url + 'flash/flash.swf' 
    end 
end 

後者的作品只有當你有你的routes.rb文件root路線。它會指向您網站的根目錄(例如http://example.com/),它基本上是public文件夾。

+0

根斜槓做了伎倆,謝謝。 – chrishunt 2010-06-16 18:39:50

3

另一種方法是擴展ActionView::Helpers::AssetTagHelper,如果您使用資產服務器,這是最有用的。這是已經實現了javascript_pathstylesheet_path的模塊。你可以做這樣的:

module ActionView 
    module Helpers 
    module AssetTagHelper 
     def flash_movie_path(source) 
     compute_public_path(source, 'flash', 'swf') 
     end 
     alias_method :path_to_flash_movie, :flash_movie_path 
    end 
    end 
end 

僅供參考,the documentationActionView::Helpers::AssetTagHelper

+0

這是優雅的。我喜歡它,謝謝。 – chrishunt 2010-06-16 18:40:59