While users have benefitted from the rise of database-driven web applications, web developers have had to implement rich functionality with technology that was not designed for this purpose. The Rails framework combines the simplicity of Ruby with the ability to rap-idly develop database-driven web applications.
Ruby on Rails is built on the philosophies of Convention over Configuration and Don’t Repeat Yourself (DRY). If you follow certain programming idioms, your applications will require minimal configuration, and Rails will generate substantial portions of your web applications for you. One of these conventions is using the Model-View-Controller (MVC) design pattern, which splits the application into the business logic aspects handled by the model and the design aspects handled by the view. The controller handles client requests by obtaining information from the model and rendering it to the view.
The MVC architectural pattern separates application data (contained in the model) from graphical presentation components (the view) and input-processing logic (the con-troller). Figure 24.10 shows the relationships between components in MVC.
The controller implements logic for processing user input. The model contains appli-cation data, and the view presents the data from the model. When a user provides input, the controller modifies the model with the given input. When the model changes, the con-troller notifies the view so that it can update its presentation with the changed data.
MVC does not restrict an application to a single view and a single controller. In a more sophisticated program, there might be two views of a document model. One view might display an outline of the document and the other might display the complete doc-ument. An application also might implement multiple controllers—one for handling key-board input and another for handling mouse selections. If either controller makes a change in the model, both the outline view and the print-preview window will show the change immediately when the controller notifies all views of changes.
The primary benefit to the MVC architectural pattern is that developers can modify each component individually without having to modify the others. For example, devel-opers could modify the view that displays the document outline without having to modify either the model or other views or controllers.
In the following examples, we show how to create a Ruby on Rails application. We show how a controller can be used to send information to the client directly, and how a control ler can render a view for a cleaner and more organized design. We then show how to set up a database in a Ruby on Rails application. Finally, we show how to generate a model to be the front end of a database in a dynamic web application.
Creating a Rails Application
The Instant Rails package comes with a full install of Rails that includes ActiveRecord, ActionView, and ActionController. ActiveRecord is used to map a database table to an Object. ActionView is a set of helper methods to modify user interfaces. ActionControl-ler is a set of helper methods to create controllers. To generate an empty Rails application in Instant Rails, click the button and select Rails Applications > Manage Rails Applications... from the drop-down menu to display the Rails Applications window. In that win-dow click the Create New Rails App... button. In the console that appears, type rails Application Name at the command line to create a directory named Application Name with a prebuilt directory structure inside. For the first example, use Welcome as the application name. Figure 24.11 shows the directory structure that is automatically generated by Rails. The directories that we’ll be primarily concerned with are app\controllers, app\models, and app\views.
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.