The Tutorial: Page Two and It Shows
So, you've got Camping installed and it's running. Keep it running. You can edit files and The Camping Server will reload automatically.
Let's show something. At the bottom of nuts.rb add:
#!ruby
module Nuts::Controllers
class Index < R '/'
def get
Time.now.to_s
end
end
end
Save the file and refresh the browser window. Your browser window should show the time, e.g.
Sun Jul 15 12:56:15 +0200 2007
Enjoying The View
The Camping micro-framework allows us to separate our code using the MVC (Model-view-controller) design pattern. Let's add a view to our Nuts application. Replace the 'module Nuts::Controllers' with
module Nuts::Controllers
class Index < R '/'
def get
@t = Time.now.to_s
render :sundial
end
end
end
module Nuts::Views
def layout
html do
head do
title { "Nuts And GORP" }
end
body { self << yield }
end
end
def sundial
p "The current time is: #{@t}"
end
end
Save the file and refresh your browser window and it should show a message like
The current time is: Sun Jul 15 13:05:41 +0200 2007
and the window title reads "Nuts And GORP".
Here you can see we call render :sundial from our controller. This does exactly what it says, and renders our sundial function. We've also added a special function called layout which Camping will automatically wrap our sundial output in. If you're familiar with HTML, you'll see that our view contains what looks HTML tag names. This is Markaby, which is like writing HTML using Ruby!
Soon enough, you'll find that you can return anything! from the controller, and it will be sent to the browser. But let's keep that for later and start investigating the routes.
Continue to TheCampingTutorialThree
or
Mosey back to the beginning of TheCampingTutorial.
