12
The MVC Pattern John Abraham Model-View-Controller

Rails contoller view

Embed Size (px)

Citation preview

Page 1: Rails contoller view

The MVC PatternJohn Abraham

Model-View-Controller

Page 2: Rails contoller view

Controller

ViewModel

Request Response

Database

Page 3: Rails contoller view

Controller

ViewModel

HTMLXLM

JSON etc

HTTP Request

Database

Web Server

Page 4: Rails contoller view

C in MVC• Controller is responsible for making sense of the request and

initiates in the cycle to generate a appropriate output

• A middle man between models and views

• Makes the model data available to the view so it can display that data to the user, and it saves or updates data from the

• REST

Page 5: Rails contoller view

C in MVC cont…• A controller is a Ruby class which inherits from

ApplicationController

• ApplicationController inherits from ActionController::Base,

which defines a number of helpful methods

• Params Hash - query string & post data

• Eg: “GET /clients?id=1&name=Joe” params = {‘id’ => ‘1’,

‘name’ => ‘Joe’}

Page 6: Rails contoller view

HostName Port (default 80)

Controller Name

Action Name

REST URL

id

class User < ApplicationController

before_action :authenticate

def showid = params[‘id’]…… endend

Page 7: Rails contoller view

V in MVC

Page 8: Rails contoller view

V in MVC

• Views represent the user interface of your application.

• In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data.

• Views handle the job of providing data to the web browser

Page 9: Rails contoller view

ERB

<html><head>………</head><body>

Hi, this is <%= @name %>.

</body></html>

Page 10: Rails contoller view

Directory Structure

class UserController < ApplicationController

def show @name = ‘John’ …… endend

controllers/user_controller.rb

<h1>Hi this is <%= @name %>.</h1>

views/users/show.html.erb

Page 11: Rails contoller view

Layouts

<html><head></head><body><%= yield %></body></html>

<h1>Hi this is <%= @name %>.</h1>

<html><head></head><body>Hi, this is John.</body></html>

Page 12: Rails contoller view

Thank You …