Let us first have a brief overview of Ruby on Rails:- Ruby on Rails is a framework for the Ruby language, which is used for rapid development of web applications. It does this by allowing you to concentrate on solving your clients’ problems and not getting caught up in building an infrastructure to support your clients problem.
Let’s face it, you don’t want to build database access layer every time you start a new project. Neither will you want to implement a fully functioning MVC pattern on a regular basis. This is the whole point of frameworks; they give you a starting point from which you can build upon. Little more about MVC:- It’s worth mentioning MVC (Model-View-Controller) because it can be one of the factors that discourages people from trying frameworks like Rails. It is rather an advanced way of programming most famous architectural style.
Lets see them in brief:- MODEL:- The model layer is where you define classes for the data your application will use/store. For instance, if you want to store posts for a blog, you will have a “Post” model. The model has the capability to interact with the database, to retrieve and store data. This functionality is gained by inheriting it from the ActiveRecord super class. Any methods, which act upon this data, should also be placed in the model. VIEW:- The view layer has one main purpose – to return the relevant.
HTML to be rendered on the users browser. In Rail a view is held in an erb (Embedded Ruby) file, which contains both HTML and embedded Ruby statements. CONTROLLER:- Without the controller, nothing would happen. The controller interacts with the model to retrieve and store data. It will then pass any data, acquired from the model, to the view. The view returns the resulting HTML to the controller and the controller sends this back to the users browser. CREATING A BLOG FROM SCRATCH USING RUBY ON RAILS:- To create a new project in Ruby On Rails(ROR),you need to type the following command from terminal or command prompt.
Ruby on Rails new myblog -d mysql //-d mysql specifies rails to use mysql.Default it uses sqllite. Requirements:- –>It should create a blog and allow the user to write the posts. –>Other users can comment on posts. //many lines can be added here based on requirements APPROACH:- –>We need to have model for both Posts and Comments. –>We also need to define relationship between these models i.e- :one to one :one to many :many to many etc SCAFFOLDING:- –>Scaffold in rails is a script which will generate suitable controllers,models,view based on used parameters and rails command.
We can use this command to create our post and comment rails generate scaffold post title:string body:text rails generate scaffold comment name:string body:text post:reference Relating the Post and Comment Models:- –>Now as the two models are created,we need to define relationships between models Post.rb class Post < ActiveRecord::Base has_many :comments end Comment.rb class Comment < ActiveRecord::Base belongs_to :post end TO CREATE A DATABASE AND TABLES:- Now that we have created the models, we need to create the database then create tables to hold information about posts and comments.
To create the database we need to add the username and password into database.yml file i.e:- development: adapter: mysql encoding: utf8 reconnect: false database: myblog_development pool: 5 username: root password: yourrootpassword host: localhost – – > After we have configured the database we need to create the database:- cd myblog //from command prompt rake db:create Database Migration In Ruby on Rails, a Migration is the name given to the process of moving your database from one state to another.
Some examples of migrating your database from one state to another are:
- Creating tables
- Removing tables
- Adding new fields
- Removing fields
When you created the two models for posts and comments using scaffold, several files were created automatically. In particular, migration files were created in the dbmigrate folder. These files are used to create the tables for the post and comment models. To build the tables using the migration files you should run the following command: rake db:migrate The “posts” and “comments” tables should now be created. SEE RESULTS:- –>Yes thats it!!!
we have created our project and we can see the results.Just start the server from terminal and give:- rails s or rails s -p 3001 // -p option is used to use a different port,default it will use port 3000. –>Now open the browser and see the result at http://localhost:3000.Before that we need to modify the default routes in configuration/routes.rb file and change the routes path accordingly.