(Advanced) Web Application Development Views in Rails Applications Bruce Scharlau, University of Aberdeen, 2017
Discuss what you know about views with person next to you Bruce Scharlau, University of Aberdeen, 2017
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
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
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
Views are generated for models Show.html.erb displays a single recipe Bruce Scharlau, University of Aberdeen, 2017
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
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
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) %> <%= email_field(:user, :address) %> <%= color_field(:user, :favorite_color) %> <%= time_field(:task, :started_at) %> <%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %> <%= range_field(:product, :discount, in: 1..100) %> http://guides.rubyonrails.org/form_helpers.html Bruce Scharlau, University of Aberdeen, 2017
Form_for binds form to object http://guides.rubyonrails.org/form_helpers.html Bruce Scharlau, University of Aberdeen, 2017
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
Parameters are passed as array Bruce Scharlau, University of Aberdeen, 2017
Use drop-down lists, etc as needed We covered these in early practical sessions Collection_select Collection_radio_buttons http://homepages.abdn.ac.uk/b.scharlau/pages/teaching/CS5550/practicals/practical-2-rails-start-travelagent.shtml Bruce Scharlau, University of Aberdeen, 2017
Rails API pages offer options Use API to find more options and details about how to use views http://api.rubyonrails.org Bruce Scharlau, University of Aberdeen, 2017
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
Render only headers if required Pick option as needed to display result http://guides.rubyonrails.org/layouts_and_rendering.html Bruce Scharlau, University of Aberdeen, 2017
Render format as needed You don’t have to return HTML. render xml: @product render json: @product 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
Discuss two key points about views with person next to you Bruce Scharlau, University of Aberdeen, 2017