Bruce Scharlau, University of Aberdeen, 2017

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Advertisements

(Advanced) Web Application Development Test Driven Development with Ruby and Rails Bruce Scharlau, University of Aberdeen, 2013.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
Chapter 15 © 2010 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
Web Applications Development Using Coldbox Platform Eddie Johnston.
Ruby on Rails by Manik Juneja Ruby On Rails. Ruby on Rails by Manik Juneja Rails is a Web Application development framework. Based on the MVC pattern.
27-Jun-15 Rails. What is Rails? Rails is a framework for building web applications This involves: Getting information from the user (client), using HTML.
ASP.NET Programming with C# and SQL Server First Edition
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
UNIT-V The MVC architecture and Struts Framework.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming.
RUBY ON RAILS Mark Zhang. In this talk  Overview of Ruby on Rails  Core ideas  Show a tiny bit of example code  Touch on several general web development/
Ruby on Rails Charles Severance Textbook: Build Your own Ruby on Rails Application by Patrick Lenz (ISBN: )
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
Chapter 4.1 – Deeper into Rails Maciej Mensfeld Presented by: Maciej Mensfeld Deeper into Rails mensfeld.pl github.com/mensfeld senior.
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
Lecture 19 Web Application Frameworks Boriana Koleva Room: C54
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch February CSCE 740 Software Engineering.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Ruby on Rails By S. Christopher Hellriegel. Overview 1. What is Ruby on Rails? 2. What is MVC? 3. Simple example 4. Wow, that was cool!
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Rails and routing INFO 2310: Topics in Web Design and Programming.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Web Technology Solutions
Jim Fawcett CSE686 – Internet Programming Spring 2014
J2EE Platform Overview (Application Architecture)
Progress Apama Fundamentals
Introduction to .NET Florin Olariu
Selenium and Selenium on Rails
The Client-Server Model
Jim Fawcett CSE686 – Internet Programming Spring 2012
z/Ware 2.0 Technical Overview
1993 version of Mosaic browser.
Play Framework: Introduction
AVOIR -African virtual
Zend_Layout & Zend_View Enhancements
Introduction to Programming the WWW I
Haritha Dasari Josue Balandrano Coronel -
CO6025 Advanced Programming
Intro to PHP & Variables
SharePoint Cloud hosted Apps
MVC Framework, in general.
Ruby on Rails by Manik Juneja
WEB API.
(Advanced) Web Application Development
Client side & Server side scripting
Ruby on Rails by Manik Juneja
Building Web Applications
Lecture 1: Multi-tier Architecture Overview
Chapter 15 Introduction to Rails.
Brian Kotek INDUS Corporation
Planning and Storyboarding a Web Site
ASP.NET MVC Web Development
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Bruce Scharlau, University of Aberdeen, 2017 Advanced Web Application Development Ruby on Rails – A walk through an app Bruce Scharlau, University of Aberdeen, 2017

What do we know about Rails? Discuss with your neighbour what you like about Rails Bruce Scharlau, University of Aberdeen, 2017

Bruce Scharlau, University of Aberdeen, 2017 About This Lecture Form ~40 minutes lectures Question time Learning Material The Hartl Book Agile Web Development with Rails Chapters 1~6 Bruce Scharlau, University of Aberdeen, 2017

Bruce Scharlau, University of Aberdeen, 2017 What is Rails Ruby on Rails (or Rails, RoR) open source Ruby framework developing database-backed web applications Rail's guiding principles: less software faster development easier to understand, maintain, and enhance convention over configuration Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden. Convention over configuration means an end to verbose XML configuration files--there aren't any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know! Bruce Scharlau, University of Aberdeen, 2017

There are many benefits to Rails Best Technologies MVC: Model View Controller Convention over Configuration DRY: Don’t Repeat Yourself REST: Representational State Transfer TDD: Test Driven Development Continuous and Active Development Version 1.0 in 2005 (We started teaching it in 2006) Helpful Online Communities Bruce Scharlau, University of Aberdeen, 2017

Bruce Scharlau, University of Aberdeen, 2017 First Touch with Rails Create a new application Rails new story Generate a controller (and its view) Rails generate controller story index app/controllers/story_controller.rb app/view/story/index.html.erb Bruce Scharlau, University of Aberdeen, 2017

Rails ‘new’ command creates app rails new cookbook generates a complete directory tree of folders and files for an empty Rails application Create models of the objects in the database, and Controllers which determine what happens in the application Bruce Scharlau, University of Aberdeen, 2017

Creates standard directory structure controllers subdirectory where Rails looks to find controller classes views subdirectory holds the display templates to fill in with data models subdirectory holds the classes that model and wrap the data helpers subdirectory holds any helper classes used to assist the model, view, and controller classes Create models of the objects in the database, and Controllers which determine what happens in the application Bruce Scharlau, University of Aberdeen, 2017

Start the built in web server rails server http://127.0.0.1:3000 Each application has built-in development server for using in the app that sits under app/script/server just call it with ‘ruby script/server’ and it starts up on port 3000 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

Bruce Scharlau, University of Aberdeen, 2017 MVC eases development Scalability (the ability for an application to grow) separate the database from other components Easier maintenance components have a low dependency on each other Reuse a model may be reused by multiple views, and vice versa 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

Add controllers for the logic rails generate controller story index Provides a class to direct requests and responses Convention: The folder that stores the template is named after the controller File Generated: app/controllers/story_controller.rb app/view/story/index.html.erb Bruce Scharlau, University of Aberdeen, 2017

We could put everything in the story/index view <blockquote>In a(n) <%=Faker::Hipster.word %> <%= Faker::Job.field %> a young <%= Faker::Hobbit.character %> stumbles across a(n) <%= Faker::Lovecraft.tome %> which spurs him into conflict with <%= Faker::StarTrek.villain %> and her <%= Faker::Hacker.noun %> culminating in <%= Faker::Pokemon.move %> where someone shouts '<%= Faker::Simpsons.quote %>' .</blockquote> Bruce Scharlau, University of Aberdeen, 2017

This works but has issues What problems are there if we do everything in the view? 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

StoryController has index method Request: http://127.0.0.1:3000/story/index StoryController is a class index is a method, or an action, which returns @story variable Convention: An html template (view) has a one-to-one mapping to the method of a controller Bruce Scharlau, University of Aberdeen, 2017

Story ‘index’ View is simple app/views/say/index.html.erb: <h1>Tell me a story...</h1> <p>Refresh the page for a new story</p> <blockquote><%= @story %></blockquote> Bruce Scharlau, University of Aberdeen, 2017

Understand index.html.erb <h1>Tell me a story...</h1> <p>Refresh the page for a new story</p> <blockquote> <%= @story %> </blockquote> Convention: html.erb is the extension of standard HTML template that allows ERB tags <%=…%>: this ERB tag pair is for displaying the output of the Ruby expression @story Bruce Scharlau, University of Aberdeen, 2017

The other story/index syntax <blockquote>In a(n) <%=Faker::Hipster.word %> <%= Faker::Job.field %> a young <%= Faker::Hobbit.character %> stumbles across a(n) <%= Faker::Lovecraft.tome %> which spurs him into conflict with <%= Faker::StarTrek.villain %> and her <%= Faker::Hacker.noun %> culminating in <%= Faker::Pokemon.move %> where someone shouts '<%= Faker::Simpsons.quote %>' .</blockquote> The <%= somevalue %> syntax lets us display the value in the view The alternative #{Faker::Pokemon.move} is for when you want the value inside a string Bruce Scharlau, University of Aberdeen, 2017

The story is displayed very simply Request: http://127.0.0.1:3000/story/index Bruce Scharlau, University of Aberdeen, 2017

Why is it better to generate the story in the controller? Discuss with the person next to you why it’s better to generate the story in the controller Bruce Scharlau, University of Aberdeen, 2017

Routes handles the URL matching User request http://127.0.0.1:3000/story/index In config/routes.rb get "story/index” shorthand for the following code: match ‘story/index’ => ‘story#index’ via => :get More details about Rails routing http://guides.rubyonrails.org/routing.html Try “rake routes” in the command line to see all routing information for an application Bruce Scharlau, University of Aberdeen, 2017

What about controllers with models? Discuss with the person next to you what controllers might do that work with models and views. What will the controller do automatically for you? Bruce Scharlau, University of Aberdeen, 2017

Controllers for models are configured automatically Generate REST methods for you Private for ’create’ Bruce Scharlau, University of Aberdeen, 2017

Model generation is easy rails generate model Recipe name:string email:string Generate the following two files: db/migrate/2017xxxxx_create_recipes.rb app/models/recipe.rb Run the database migration by rake db:migrate 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

Databases use Migration rake db:migrate Rake is Ruby make, a make-like language written in Ruby for performing tasks Rails comes with built-in support for SQLite3 SQLite3 is a lightweight serverless database application for development Rails can support other database system too Bruce Scharlau, University of Aberdeen, 2017

Database Configurations config/database.yml YML: lightweight format used in Rails for configuration development: adapter: sqlite3 database: db/development.sqlite3 timeout: 5000 test: database: db/test.sqlite3 timeout: 5000 production: database: db/production.sqlite3 timeout: 5000 If you use sqlite3, you don’t need to do anything, everything is already configured. Bruce Scharlau, University of Aberdeen, 2017

The Model file can be basic class Recipe < ApplicationRecord end In a controller, you can use: Recipe.all retrieve all recipies Recipe.find_by_name(XXXX) retrieve a recipe whose name is XXXX Bruce Scharlau, University of Aberdeen, 2017

Scaffolding generates MVC rails generate scaffold recipe title:string description:string date:date instructions:text 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

Bruce Scharlau, University of Aberdeen, 2017 Summary Ruby on Rails offers quick development of data driven web applications Rails is based on MVC design pattern Bruce Scharlau, University of Aberdeen, 2017