Advertisement

Ruby on Rails

Ruby on Rails Generate Controller & View via rails command

Controller | Mvc | Ruby On Rails | View

Learn Ruby on Rails

Advertisement

This entry is part 4 of 4 in the series Learning Ruby on Rails From Scretch

In Ruby on Rails generate controller & view can be done via rails’ generate command. First you have to understand MVC Architecture. if you don’t know about MVC, please refer to our MVC Architecture Tutorial. which explains what is MVC and why it is used widely. In the last tutorial we created a rails project. We will continue with that first_project. If you haven’t checked the previous tutorial please refer it before continuing work on this.

If we check our root directory of our project (which is first_project directory), we can find few directories & files created by the rails’ new command.

Important Directories created by the Rails’ new command.

  • App (main area where we spend our most of the time: we code in this directory).
  • Config (configuration files for rail project).
  • DB (database related files).
  • Lib (Library files).
  • Log.
  • Public (files that does not need to processed by rails server like static html files).

In this tutorial, we will create our first controller in rails’ project. Before we create controller lets see few commands first. Open terminal or command prompt and navigate to your project’s root directory. Now try to follow commands.

This will give you options that generate will accept like controller, helper, model etc. Now try another command.

This will give you manual for generating controller command. By looking at the manual, we can easily see that we have to pass the controller name and optionally view names that needs to be generated.

Ruby on Rails Generate Controller & View

Now let’s create our first controller named demo.

After running above command, a demo controller will be generated and 2 view files will be created named index and hello. You can specify as many views required for your controller by separating names by spaces. If you open demo_controller.rb file, stored in the app/controllers directory, generated by rails. It looks like below one.

Ruby on Rails Generate Controller

Demo Controller contains code for 2 methods index & hello. Views directory contains 2 files inside the demo directory with index & hello names. by default if we open browser and navigate to http://localhost:3000/demo/index. result will be like this:

Ruby on Rails Generate Controller

We didn’t write any code to show above output but rails write default code in every view file. Try loading http://localhost:3000/demo/hello and you will get a slightly different text. Now let’s modify the index view file (index.html.erb). Open in your favorite editor and modify it so it looks like this.

Now run again in the browser and check  result yourself. So far so good. Now lets understand how everything works. Whenever we generate controller rails automatically write some code in our config/routes file. If you open it in an editor, you will get a line on top of the file like below.

Ruby on Rails Generate Controller Routes

Line get “demo/index” indicates that whenever it gets a request from demo/index url. It will pass it to demo controller’s index method. And then by default that method renders view named index in demo directory (views/demo/index.html.erb).

In the next tutorial we will discuss about rendering views & templates. Like our FB Page for updates on programming tutorials & solutions.

 

Series Navigation<< Ruby on Rails Render Template & Pass Data to View

Bookmark Link using: bookmark at folkd