Download presentation
Presentation is loading. Please wait.
Published byRachel Conceição Bardini Modified over 6 years ago
1
(Advanced) Web Application Development
Views in Rails Applications Bruce Scharlau, University of Aberdeen, 2017
2
Discuss what you know about views with person next to you
Bruce Scharlau, University of Aberdeen, 2017
3
Bruce Scharlau, University of Aberdeen, 2017
MVC is a design pattern Model represents the data in the program and business logic View (templates) manages the visual display of the data (Presentation Logic) Controller provides the means for user interaction with the data (application logic) Model View Controller Bruce Scharlau, University of Aberdeen, 2017
4
A Rails request uses MVC
Browser Model Controller View The browser sends a request for a page to the controller on the server. The controller retrieves the data it needs from the model in order to respond to the request. The controller renders the data and sends it to the view. The view sends the page back to the client for the browser to display. Bruce Scharlau, University of Aberdeen, 2017
5
Keep domain logic out of views
The view is there to support the presentation of the model. Any Ruby code should be in the model, a helper, or in the controller. Bruce Scharlau, University of Aberdeen, 2017
6
Views are generated for models
Show.html.erb displays a single recipe Bruce Scharlau, University of Aberdeen, 2017
7
Views are generated for models
_form.html.erb is for ‘new’ and ‘edit’ recipe files For loop gathers each attribute value of model Bruce Scharlau, University of Aberdeen, 2017
8
Views are generated for models
index.html.erb is all recipe items For loop goes through each item in array Bruce Scharlau, University of Aberdeen, 2017
9
Explore Views built-in helpers
<%= text_area_tag(:message, "Hi, nice site", size: "24x6") %> <%= password_field_tag(:password) %> <%= hidden_field_tag(:parent_id, "5") %> <%= search_field(:user, :name) %> <%= telephone_field(:user, :phone) %> <%= date_field(:user, :born_on) %> <%= datetime_local_field(:user, :graduation_day) %> <%= month_field(:user, :birthday_month) %> <%= week_field(:user, :birthday_week) %> <%= url_field(:user, :homepage) %> <%= _field(:user, :address) %> <%= color_field(:user, :favorite_color) %> <%= time_field(:task, :started_at) %> <%= number_field(:product, :price, in: , step: 0.5) %> <%= range_field(:product, :discount, in: ) %> Bruce Scharlau, University of Aberdeen, 2017
10
Form_for binds form to object
Bruce Scharlau, University of Aberdeen, 2017
11
Use ‘form_tag’ for regular forms
This is not tied to a model, so can gather data as required. Bruce Scharlau, University of Aberdeen, 2017
12
Parameters are passed as array
Bruce Scharlau, University of Aberdeen, 2017
13
Use drop-down lists, etc as needed
We covered these in early practical sessions Collection_select Collection_radio_buttons Bruce Scharlau, University of Aberdeen, 2017
14
Rails API pages offer options
Use API to find more options and details about how to use views Bruce Scharlau, University of Aberdeen, 2017
15
Layouts are to be rendered
You can render the expected view by default. You can also change the rendered view. Instead of ‘show’ you could use ‘edit’ or something else as required. Bruce Scharlau, University of Aberdeen, 2017
16
Render only headers if required
Pick option as needed to display result Bruce Scharlau, University of Aberdeen, 2017
17
Render format as needed
You don’t have to return HTML. render render render html: "<strong>Not Found</strong>".html_safe render plain: “ok” render body: “raw” When and why would you use these? Bruce Scharlau, University of Aberdeen, 2017
18
Discuss two key points about views with person next to you
Bruce Scharlau, University of Aberdeen, 2017
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.