Upload
john-abraham
View
46
Download
0
Embed Size (px)
Citation preview
The MVC PatternJohn Abraham
Model-View-Controller
Controller
ViewModel
Request Response
Database
Controller
ViewModel
HTMLXLM
JSON etc
HTTP Request
Database
Web Server
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
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’}
HostName Port (default 80)
Controller Name
Action Name
REST URL
id
class User < ApplicationController
before_action :authenticate
def showid = params[‘id’]…… endend
V in MVC
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
ERB
<html><head>………</head><body>
Hi, this is <%= @name %>.
</body></html>
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
Layouts
<html><head></head><body><%= yield %></body></html>
<h1>Hi this is <%= @name %>.</h1>
<html><head></head><body>Hi, this is John.</body></html>
Thank You …