Saturday, October 2, 2010

LogU-Designing the MVC in Rails

 I am only providing some key information that will be helpful in other projects.

Creating the Model

$rails generate model Article
  This will create a model and a database named article. To add rows in the db we have to edit the db/migrate.*.rb file. Then again migrating the database with rake will create a database table with the given fields.

After creating the models we can add data to the db and also check the model in interactive mode my typing
$rails console

Relationship between different db models can be shown by
        >has_one
        >has_many
        >belongs_to
        >has_and_belongs_to_many

Generating the controller

$rails generate controller article
    It generate a controller named ArticleController in app/controller. We can add our template files for the corresponding controller in app/view/articles. We also have to change the route.rb file in config corresponding to the model and controller.

Scaffolding is one of the important feature of rails. Scaffolding helps to create a set of actions and templates that makes it easy to manipulate data for a specific model.

Creating the View
        There is no need to create or generate a view. At the time of  generating the controller a view is also generated. We can add CSS to the file inside app/views/layouts which can be applied to view of each page.

You can find the code in http://github.com/omalbastin/logu
and run it on  http://logu.heroku.com/

No comments:

Post a Comment