O'Reilly logo

Learning Sinatra by Manoj Sehrawat, Sudeep Agarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Where is the MVC in this code?

Sinatra does not follow the MVC architecture completely; it is a VC framework. It was mentioned earlier that the model is the data part of the application, view is the user interface, and controller is the interconnection. Now, let's find these parts in the preceding code:

1 require 'sinatra'
2
3 get '/' do
4   return 'Hello World!'
5 end

Here, line 1 requires just the gem. Lines 3 to 5 define a code-block that is invoked on receiving a get request and returns a value. So here, this code-block forms the controller. It controls what to return when a request is received.

The view is the page rendered by the browser. We do not have a separate view here but line 4 defines the contents of the view.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required