How to Ruby on Rails- Create sample app

Now that you have Rails installed, you need to generate a basic application. Ruby on Rails uses code generation for several tasks, most typically to create a new Rails application, as well as setting up the Controller and models.

Create Sample App

In this guide, We’re going to create a new Rails app called lauyou.

First, let’s open the command prompt with Ruby and Rails from RailsInstaller.

Now you are in Ruby and rails command prompt within the folder Sites.

Rails Command Prompt

Now you want to create a new app called lauyou by running

rails new lauyou

Result

On the command line again, run bundle install. This tells bundler, a library used by Rails, to fetch and install all the libraries it needs. While that’s running, take the opportunity to look around the generated Rails application. This will create a new app in the folder lauyou, so we again want to change the directory to be inside of our rails app by running

cd lauyou

If you run ls inside of the directory you should see folders such as app and config.

Directory list

You can then start the rails server by running.

rails server

It will start up the built-in web server that ships with Rails, running it on port 3000. On the command line,

Start Server

Now, open your browser and point it to http://localhost:3000, you should now see a welcome page like this

Rails Welcome page

Generate home controller & view

Open another Ruby and Rails command prompt with from RailsInstaller. Change the directory to be inside of our rails app lauyou.

Run the following command to generate a home controller and a view ‘index’.

rails generate controller home index

Here, home is the controller and index is the view of home controller.

Create controller

Edit “routes.rb” file to set home to root directory. Locate route.rb from Sites/lauyou/config.

Add the following line to route.rb,

root :to => 'home#index'

Above code route views/home/index as the root directory of our app.

routers.rb

Let’s check our app by browsing localhost:3000. You will see the home page is fetched from views/home/index.html.erb.