Ruby on Rails app from scratch

Ruby on Rails app from scratch

Ruby on Rails framework has numerous advantages. One of them is faster development of websites. Today we are going to build a website for managing a book store. For now our goal will be to setup the basic website, where you can add some book details. So let's begin.

For this particular website I am using Rails 5.1 and ruby 2.3.0. You can you any of the version you wish to work on.

Now to start with, we need to create a rails application structure. Fortunately we are having many in-built scripts of rails which help us with initial setup of our application folder. To create a new rails application we need to run following command:

  rails new book_store -d=postgresql
        

This command allows you to create a new rails application named, book_store, using PostgreSQL for the database. If you skip the option "-d=postgresql", it will create the application using SQLite for database. This command with setup the basic structure for your application, including MVC. You may go through some initial setup like, Gemfile, routes, database.yml file, etc.

Next step to do is to create the database for the application by running following command:

  rake db:create
        

This command will create the database for your application using the settings mentioned in database.yml file. On running this command you will see that it creates 2 database - one for development mode and another for test mode.

No alt text provided for this image

Now you are all set to run the rails server using following command:

  rails server
          

Now when you go to the browser and open new tab and enter 'localhost:3000', you will see that your application is running, as shown below.

No alt text provided for this image

Now that your application is running, but you still don't have something to work with on this application. So lets add something meaningful to this website. Lets start by adding model(database table), controller and views for books, using following command:

  rails g scaffold Book name author_name genre publication_name
            

Yes the above single command will add model, view, controller, routes, etc. This is also one of the in-built generator of rails, which helps in faster development. In this command "Book" is the name of model and all the rest of argument after that are the name of the fields of the model. By default the data type of the field will be string. Let say you wish to add some field with boolean or integer datatype, you can use following syntax

  rails g scaffold ModelName field_name:datatype (field_name:datatype ....)
          

On running the scaffold with generate the files in the application folder as shown below:

No alt text provided for this image

Next step will be to migrate the model created using scaffold, using following command:

  rake db:migrate
        

Now when you start the server and visit 'http://localhost:3000/books' you will see following:

No alt text provided for this image

This is the index pages where all the books will be listed down, when you add them from following screen:

No alt text provided for this image

Here you are with your basic ruby on rails application where you can add/update/read/delete your book details. You can play around with the application and find out how you can manage your own library or wishlist of books that you want to read.

In next article you will be able to add validations.

Enjoy!!

To view or add a comment, sign in

More articles by Vishal Malukani

  • Authentication in ruby on rails

    Hello Friends. Till now we have seen to setup a basic application and applying validations in previous articles.

  • Ruby on Rails Validations

    In continuation of previous article, where we created a basic rails application, let continue to add validations to the…

    1 Comment

Others also viewed

Explore content categories