Introduction

Welcome to my Ruby on Rails article. I assume you have already read my previous article on creating applications in Ruby On Rails. Now, you will learn how to generate a Controller and View in Ruby On Rails. We will learn about the MVC web, Rails Architecture, Controller, Views, Route and its types.

MVC Web Architecture

Generate A Controller And View In Ruby On Rails
The browser sends request to controller, the controller decides and accesses the model and database. If the request need if find that file, its ready to send on view, the view sent back file into the web browser as requested page. This is the process its continue on each request the browser communicates with controller, the controller goes to the view, the view goes back to the browser
Requirements
Step 1
    1. Rails new simple_app1
Step 2
    1. Rails Generate Controller Home Index
Step 3
Step 4
Step 5
Step 6
    1. Rails s

      Generate A Controller And View In Ruby On Rails
  • After you start Rails server, the puma server application starts booting the server. Your controller and Views files start development on localhost:3000 but your HTML file is located in sub directory so you add home/index in localhost:3000 http://Localhost:3000/home/index.

    Generate A Controller And View In Ruby On Rails

Server Request Handling

  • How server request works and how to handle that request.

Rails Architecture

Generate A Controller And View In Ruby On Rails
Step 1
Step 2
Step 3

Routing

Route Types
Simple Route Route
Shorthand code
  1. Get'Home/Index'
Longhand code
  1. Match 'Home/Index'; :To => 'Home#Index', :Via => :Get
Step 1
Step 2
Step 3

Default Route Structure

:Controller/:Action/:Id
Example

Get/Home/Index
Definition of Example

Home-Controller,Index-Action.
Default Route
  1. Get ’ :Controller(/:Action(/:Id) ) ’
  2. Match ‘ :Controller(/:Action(/:Id)’, : Via => :Get
Step 1
Step 2
Step 3
    1. Get ‘: controller(/:action(/:id))’

      Generate A Controller And View In Ruby On Rails
  • Now, user requests that the controller checks in routing file but there is not a manual routing code, there is only a default routing code so it takes default action in that request .
Step 3

Root Route

The root route is used to start at that file like home page in localhost:3000.
Longhand code
  1. Match “/” , : To => ‘Home#Index’, : Via => :Get
Shorthand code
  1. Root ‘Home#Index’
Step 1
Step 2

Conclusion