4 easy steps to create a simple web app with Ruby On Rails!

Aside

Ruby on Rails is an open source web development framework useful in creating powerful web applications. We will be using a technique known as ‘scaffolding’ to create simple RoR in 3 easy steps!

Before we begin, do you have Rails server installed?

If yes, you’re cool ūüôā If no, please do it now.

Ruby On Rails Installation  

Follow the simple instructions from: http://installrails.com/

Hope you have installed Ruby on Rails in your Linux / Windows / Mac machine!  If you have any difficulties, feel free to post on the comments.

Let’s kick-start!

 

Step #1: Create a new app directory 

Create a new directory for our rails app with the following command through command line.

$ rails new webapp

The command creates a new directory with the name ‘webapp’ (as per the last part of the command).

Move inside the newly created directory with the following command.

$ cd webapp

And we’re done with the first step!

Start running the Rails server using the following command in the command line:

$ rails s

Keep the server running on one of the Terminal tabs throughout all the steps.

Open your browser, and visit the following URL:

localhost:3000/

Minimize it, and dive into the following steps.

Step #2: Using generate scaffold command

Here comes the important part! Use the following command for scaffolding. ¬†Description coming below ūüėČ

$ rails generate scaffold post name:string college:text

For any web app to run on a Ruby on Rails environment, we require Model, View and Controller (MVC).  The scaffold command is the easiest way of creating all of these at once.

The above command will generate a post model with two fields: name and college. ¬†Remember, you can create any number of fields using the above command. ¬†The data specifications of the fields are described after the field name separated by a ‘:’.

Step #3: Run the migration command

Proceed with running the following in the command line.

$ rake db:migrate

The command¬†will migrate the real-time changes you’ve made into the database.

Step #4: Edit routes.rb file

Once you are done with the migration, let’s go ahead with editing the contents of routes.rb file. ¬†Follow the commands,

cd config
vi routes.rb

Add the following line in the routes.rb file.

root :to => "posts#index"

Save and Exit ( with Esc -> :wq -> Enter ).

This will enable the ‘/’ to route to the index of PostsController.

That’s it!¬†Open the browser again. ¬† Go to the URL:

localhost:3000/

If you’re already there, just refresh.

 

See the magic you have done! Your first RoR app has been successfully created. Cheers! Play with the CRUD operations (Create, Read, Update, Delete) available in the web app.

Well, if you’re not seeing the magic, then you might went wrong with any of the above steps. ¬†Try debugging or start from fresh or let us know with some explanation of what you did or where you went wrong¬†in the comments. ¬†We will try fixing it!

Thanks for reading. ūüôā

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s