Getting Started
If you haven’t installed Ramaze yet, follow the installation guide and return to this page.
Hello, Ramaze
require 'rubygems'
require 'ramaze'
class MainController < Ramaze::Controller
def index
'Hello, World!'
end
end
Ramaze.start
Put the above code in a file like ‘hello.rb’, then
$ ruby hello.rb
In your browser visit http://localhost:7000
Simple, isn't it? No ./script/generate or any special preparation is needed. Ramaze applications are Ruby programs, so you just write normal Ruby code.
Lights, Camera, Action!
The index action is called by default when no path is provided in the URI beyond the hostname. Below, accessing http://localhost:7000 invokes this index method and the return value, “Hello World” is rendered to the browser.
class MainController < Ramaze::Controller
# http://localhost:7000
def index
"Hello, World!"
end
end
Controllers can be mounted onto different URL paths with the map directive. A mapped controller will only respond to requests that are prefixed with the supplied mapping. Below, http://localhost:7000/another/hello will invoke AnotherController’s method hello.
class AnotherController < Ramaze::Controller
map '/another'
# http://localhost:7000/another/hello
def hello
"Hello from Another!"
end
end
Your action methods can accept arguments in two ways
- GET or POST variables
- As slash-separated parameters in the URL.
For example:
class MainController < Ramaze::Controller
def add(first, second)
# http://localhost:7000/add/A/B?number=one
# request.get? == true
# first == 'A'
# second == 'B'
# request['number'] == 'one'
end
end
Default values for method parameters, like with def add( first, second, third = "C" ), will work as expected.