Generate A Controller And View In Ruby On Rails

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
  • Software - Ruby on Rails Installed In Your System.
  • Editor- Atom Text Editor Is The Best In Class.
  • Browser – Google Chrome Web Browser.
Step 1
  • Open the Ruby On Rails Command Prompt By Going To Start >> All Programmes >> Railsinstaller >> Command Prompt with Ruby and Rails.

  • After opening the command prompt, now let’s create a new application in rails using the following code.
    1. Rails new simple_app1
  • Then some process goes on in command prompt but we skip that, if you have any doubts about application creation refer to my previous article about creating applications in ruby on rails.

    Generate A Controller And View In Ruby On Rails
Step 2
  • Now, we generate controller and view but before you generate the controller, you must enter the application folder by using simple cd/ command. Then, generate the controller using this code in Ruby on Rails command prompt.
    1. Rails Generate Controller Home Index
  • The above code defines Home as controller , Index as Views.

  • Once you enter the code, it will take some time to generate controller and view. All the supporting files and folders relate to controller, view and route files. It will be created automatically in command prompt.

    Generate A Controller And View In Ruby On Rails

    Generate A Controller And View In Ruby On Rails
Step 3
  • The controller (home) file is created successfully. Now, you can find the controller located at C:/Sites/Sample_app1/app/controllers.
  • This controller file is used to control the views files.

    Generate A Controller And View In Ruby On Rails
  • Now Add Layout False code is used rand  it will be off default rails web page in localhost:3000.

    Generate A Controller And View In Ruby On Rails
Step 4
  • The views (index) file is created successfully. Now, you can find the views file located at C:/Sites/Sample_app1/app/views/home/index.html.
  • It contains basic HTML code in that file, if you want to change it yourself.

    Generate A Controller And View In Ruby On Rails
Step 5
  • The route file is created successfully. Now, you can find the route file located at C:/Sites/Sample_app1/config/route.rb.
  • This route file helps to route files in web browser.

    Generate A Controller And View In Ruby On Rails
Step 6
  • Start the Rails server using this code in Ruby on Rails command prompt.
    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
  • Now we successfully created Controller and Views in this step.

Server Request Handling

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

Rails Architecture

 
Generate A Controller And View In Ruby On Rails
  • The request starts with the browser. It runs to the web browser for processing. The application development runs on puma web server.
  • The web server look into the application's public directory for a file which matches the browser’s request.
  • If it finds that file, it will return the file with browser, and it will never access the Rails framework at all. It’s very help full for web pages like Html.
  • Whenever the web server doesn’t find a matching file, it will ask the Rails framework to handle the file request.
  • The request goes to Rails routing to ask controller and action to return the web browser.
  • What is different with Rails is that it tries to find a file in the public directory, if it can’t find it asks the Rails application if it can handle the request.
Step 1
  • Now you going to C:/Sites/sample_app1/public directory already it has some function files. You add a New Html Page in that folder, HTML code is yours.
  • Then open that HTML file in Atom editor and write the code yourself.

    Generate A Controller And View In Ruby On Rails
Step 2
  • Before you open browser, you must start the server, after the server starts.
  • Now, you can check into your bowser and search http://localhost:3000/default_page.html the HTML will start with your HTML page.

    Generate A Controller And View In Ruby On Rails
Step 3
  • Now you create one folder in sample_app1/public directory then put that HTML file in that folder. Check the browser and search htttp://localhost:3000/sample/default_page.html.
  • The HTML file is where to get into the browser via server request handling.

    Generate A Controller And View In Ruby On Rails

Routing

  • We already saw how the routing works in previous Rails architecture. The matching file is on the public directory, but it can’t find a match; it sends requests to the routing section of Rails framework.
  • Rails routing checks URL for which controller and action will be called for route definition.
Route Types
  • Simple Route
  • Default Route
  • Root Route
Simple Route Route
 
Shorthand code
  1. Get'Home/Index'
Longhand code
  1. Match 'Home/Index'; :To => 'Home#Index', :Via => :Get
Step 1
  • Now you open the web browser and search http://localhost:3000/home/index, its works fine.

    Generate A Controller And View In Ruby On Rails
  • But If You search without index (view) file like this http://localhost:3000/home . Its shows routing error and No route matches[GET]”/home”.

    Generate A Controller And View In Ruby On Rails
Step 2
  • Now you can check if the server command prompt the GET “/home/index” is successfully rendering home/index file. But the GET “/home/” shows routing error and no route match [GET] “/home”.

    Generate A Controller And View In Ruby On Rails
Step 3
  • Now open the Route.rb file you will see the get ‘home/index’ so the index file will run without routing error in browser.

    Generate A Controller And View In Ruby On Rails

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
  • Here, the GET requests the atching file to check the path in route file and see if its' available for the controller to send to the web browser. If file path is not in route file it shows routing error.

  • Note
    I commented the actual path get ‘home/index.

    Generate A Controller And View In Ruby On Rails
Step 2
  • Now you can check in the web browser http://locaalhost:3000/home/index. It shows routing error because the routing GET ‘home/index’ command is missing in routing file so it can’t find actual path so it helps the Rails framework. It shows a routing error which means the request file is not matching in routing file.

    Generate A Controller And View In Ruby On Rails
Step 3
  • Now we add default routing code in routing file following this code
    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
  • Now, we refresh the previous tab without Index. That page doesn’t change because its request to directly check inside the home/index file will automatically route to the browser. It’s called default route action.

    Generate A Controller And View In Ruby On Rails

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’
  • The home controller will take action as first index file for actual meaning in that code.
Step 1
  • Now, put the root ‘home#index’ code and get ‘home/index’ code in route.rb file.

    Generate A Controller And View In Ruby On Rails
Step 2
  • Now, you can search localhost:3000 in browser. It shows the following page instead of the home page. This is the use of root route in Ruby on Rails.

    Generate A Controller And View In Ruby On Rails

Conclusion

  • I hope you have understood the concept of MVC web, Rails Architecture, Controller, Views, and Route in Ruby on Rails application in this article.

  • In future articles, you will learn more about Ruby on Rails. 


Similar Articles