September 11, 2007 10:36am
Camping interprets every request to the server using the paths in the controllers, so if you want to serve static files, like external stylesheets or images, you need to provide a controller for them. Take this bit of Markaby:img :src => 'images/logo.png'This will get interpreted by Camping and end up with this HTML:<img src='images/logo.png' />When the browser reads that, it will send a GET request to the Camping server looking for logo.png, that Camping will try to match against the controller paths. If you haven't set one up, you'll get an error.
So, in your Controllers module:class StaticImage < R '/images/(.*)'and in your View module, create the link like this:
def get(static_name)
current_dir = File.expand_path(File.dirname(__FILE__))
@headers['Content-Type'] = "image/png"
@headers['X-Sendfile'] = "#{current_dir}/images/#{static_name}"
end
endimg :src => R(StaticImage, 'logo.png')
Shake and bake for any kind of static resource you want to send.
October 15th, 2007 at 3:31am
this doesnt work with windows xp. ive tried this and also the one from _why's, it works fine linux but not in windoze. cant force my user to use linux for now. can someone tell me what's the deal with static files and windows or at least point me in the right direction..
many thanks ...
October 15th, 2007 at 7:29am
What doesn't work? What happens? Does Camping run okay, but static files aren't served? I know nothing about Windows, but no-one is going to be able to help you without more detail.
You might also try the #camping IRC channel.
October 15th, 2007 at 2:36pm
this is what i have:
in my views module i have:
def index
...
img( :src=>R(StaticImage, 'valid-html401.png'), :alt=>'valid-html401.png')
end
i have my 'valid-html401.png' in my images folder under my current path. "#{curdir}/images/valid-html401.png"
i also tried putting the image under the current path but still the same. it just shows the alt text, or a missing image box without the :alt param.
["@root", ""]
["@in", #]
["@status", 200]
["@method", "get"]
["@k", {"CP"=>"*", "camping_sid"=>"xOzyTZ6dUtiRAAomFpMAWMM2U9rCO6cV"}]
["@env",
{"SERVER_NAME"=>"localhost",
"HTTP_USER_AGENT"=>
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7",
"HTTP_ACCEPT_ENCODING"=>"gzip,deflate",
"PATH_INFO"=>"/images/valid-html401.png",
"HTTP_HOST"=>"localhost:3303",
"HTTP_ACCEPT_LANGUAGE"=>"en-us,en;q=0.5",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"SCRIPT_NAME"=>"/",
"REQUEST_PATH"=>"/images/valid-html401.png",
"HTTP_KEEP_ALIVE"=>"300",
"SERVER_SOFTWARE"=>"Mongrel 1.0.1",
"REMOTE_ADDR"=>"127.0.0.1",
"HTTP_REFERER"=>"http://localhost:3303/",
"HTTP_VERSION"=>"HTTP/1.1",
"HTTP_ACCEPT_CHARSET"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"HTTP_COOKIE"=>"CP=*; camping_sid=xOzyTZ6dUtiRAAomFpMAWMM2U9rCO6cV",
"REQUEST_URI"=>"/images/valid-html401.png",
"SERVER_PORT"=>"3303",
"GATEWAY_INTERFACE"=>"CGI/1.2",
"HTTP_ACCEPT"=>"image/png,*/*;q=0.5",
"REQUEST_METHOD"=>"GET",
"HTTP_CONNECTION"=>"keep-alive"}]
["@input", {}]
["@headers", {"Content-Type"=>"text/html"}]
["@cookies", {"CP"=>"*", "camping_sid"=>"xOzyTZ6dUtiRAAomFpMAWMM2U9rCO6cV"}]
many thanks...
October 15th, 2007 at 2:55pm
You define the StaticImage route in your controller? When you say this works on Linux but not Windows, do you mean you're running Camping on Windows or the browser is on Windows?
Like I said, you'll get better help at #camping (or on the mailing list, http://rubyforge.org/mailman/listinfo/camping-list).
December 7th, 2007 at 12:05pm
[...] you need to set up a route for your JQuery. I’ve talked about sending static files with Camping before, so this is just a modification of that. [You can serve static files more sensibly than [...]
December 12th, 2007 at 2:18pm
[...] written here and here about serving static files with Camping, but each time I’ve set up a new route for [...]
April 25th, 2008 at 8:25pm
I have been struggling with this too: I couldn't get the above code to work on XP either but _Why published another way which does work:
here is my modified homepage.rb script
require 'camping'
Camping.goes :HomePage
module HomePage::Controllers
# The root slash shows the `index' view.
class Index R '/'
def get
render :index
end
end
# Any other page name gets sent to the view
# of the same name.
#
# /index -> Views#index
# /sample -> Views#sample
#
class Page R '/(\w+)'
def get(page_name)
render page_name
end
end
end
module HomePage::Views
# If you have a `layout' method like this, it
# will wrap the HTML in the other methods. The
# `self
April 27th, 2008 at 12:22pm
Sorry, Jonty, I really do have to put some 'your comment in moderation' code in there.