Access Ruby on Rails 'public' directory without relative path
I have a flash object I wish to load and I believe the best pla开发者_运维知识库ce to store that asset is in the public
directory. Suppose it's stored in public/flash
, there must be a better way to path to the swf than what I've done below. Note the 'data' element, it has a relative path.
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
Is there some rails variable that points to public
?
An alternative method is to extend ActionView::Helpers::AssetTagHelper
, which is most useful if you're using asset servers. This is the module that already implements javascript_path
and stylesheet_path
. You could do it like this:
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
For reference, the documentation on ActionView::Helpers::AssetTagHelper
.
Wouldn't this work?
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
Alternatively you could use the root_url
as the prefix:
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
The latter works only if you have the root
route in your routes.rb
file. It will point to the root of your site (e.g. http://example.com/
) which is basically the public
folder.
精彩评论