rails contoller view
TRANSCRIPT
![Page 1: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/1.jpg)
The MVC PatternJohn Abraham
Model-View-Controller
![Page 2: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/2.jpg)
Controller
ViewModel
Request Response
Database
![Page 3: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/3.jpg)
Controller
ViewModel
HTMLXLM
JSON etc
HTTP Request
Database
Web Server
![Page 4: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/7.jpg)
V in MVC
![Page 8: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/9.jpg)
ERB
<html><head>………</head><body>
Hi, this is <%= @name %>.
</body></html>
![Page 10: Rails contoller view](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022071923/55d1e7bdbb61eb71548b45c2/html5/thumbnails/12.jpg)
Thank You …