Pr-request for Ruby On Rails Tutorials : Use MVC (Model View Controller)

Read our previous post Ruby On Rails Tutorials : Creating new project in Rails

In our previous post we create a new project in Ruby on Rails. Now moving towards our next step, where we will learn how Ruby uses MVC (Model View Controller) . We will also learn how to build dynamic pages in ROR(Ruby On Rails) using Ajax, java and .Net developers already know how Ajax works. But it’s time to do it in ROR. Let’s start from generate a new controller.

Generate controller in Ruby On Rails (ROR)

When you are working with rails, you just need to focus on your real code rather than in generating and linking some files, as rails take care of that. That is also one of the reason, why I love Rails. Let’s see how ruby uses MVC (Model View Controller) . It is too easy to generate a new controller in rails. Follow the steps below.

  1. Open your terminal.
  2. Change your directory to your project using the following command.
    cd work/buffercode
  3. Now just type
     rails generate controller buffer func1 func2

    That’s it !!!

Above command will create a file buffer_controller.rb in apps/controllers/ , which is a ruby file with .rb extension. It will also create some view files in your apps/views/buffer folder, where you can write code for front-end in html,erb- which can use css,bootstrap,js etc for view.

Ok now test it !! Open your browser and type localhost:3000/buffer/func1 to test what you did here. It will open a page as shown in image below.
MVC (Model View Controller)
So now again question is how it open this page??
As we told in our previous tutorials, Rails is a MVC framework. Rails accepts incoming requests from a browser, decodes the request to find a controller from controllers and calls an action method in that controller. Ok now we will show you, magic of Ajax and embed ruby code.

Embed Ruby Code (erb)

If you check your buffer folder inside apps/views, there you will find two files func1.html.erb and func2.html.erb . These are not simple html files, instead you can embed your ruby code there like you use in jsp and asp. Let’s see an example of it. Follow the below steps.

  1. Edit apps/views/buffer/func1.html.erb in your favorite editor( either use vim or gedit).
  2. Write the below code in it and save it.
    <h1>Tutorial from Buffercode :Embed Ruby Code</h1>
    <p>Let's check the time now</p>
    <p>It is now <%= Time.now %></p>
  3. Now just refresh your browser and you will be able to see your current time. It will just call the method now from class inbuilt ruby class Time.

There is no need to restart the application after changing the code. It has been chugging away in the background. And yet each change we make is avail whenever we access the application through a browser. What gives?
In development mode, it(Rails dispatcher) automatically reloads application source files when a new request comes along. That way, when we edit our application, the dispatcher makes sure it’s running the most recent changes. This is great for development.

*It cause a short pause after you enter a URL before the app responds . That’s caused by the dispatcher reloading stuff. for development it is a price worth paying, but in production it would be unacceptable. Because of this, feature  is disable for production deployment.

How to use controller in rails

Now we will use controller to get current time. Just follow these simple steps:

  1. Edit apps/controllers/buffer_controller.rb
  2. define a local variable @time=Time.now in func1 as below
    class BufferController < ApplicationController
      def func1
        @time=Time.now
      end
    
      def func2
      end
    end
  3. No edit func1.html.erb and call @time there .
    <h1>Embed Ruby Code</h1>
    <p>Let's check the time now</p>
    <p>It is now <%= Time.now %></p>
    <p>Time using controller <%= @time  %> </p>

MVC (Model View Controller)

Woooh!! it works too , it simply means we can write our business logics in controller and then can call in views.
Is not it amazing??

If you do have any question please ask in comments.

Have something to add or still stuck somewhere ?? share it in comments .

Follow us on Facebook, Google Plus and Twitter.

1 COMMENT

Comments are closed.