CSCI-2320 Web Programming: Ruby on Rails

Similar documents
Day 3: 26/April/2012 Scaffolding Generation of Skeletons; Test run Memopad

Rails: MVC in action

Lecture 4. Ruby on Rails 1 / 49

Web System Development by Ruby on Rails. Day 3(4/Oct/2012) First Project Internationalization

Rails: Views and Controllers

Lecture 4. Ruby on Rails 1 / 52

Introduction and first application. Luigi De Russis. Rails 101

Problem: Write HTML would create web page depicted below. Your solution must include the following types of HTML elements (and no other

Rails Guide. MVC Architecture. Migrations. Hey, thanks a lot for picking up this guide!

Courslets, a golf improvement web service. Peter Battaglia

Rails 4 Quickly. Bala Paranj

Rails: Associations and Validation

Rails 5 Quickly. Bala Paranj

Day 2: 19/April/2012 Installing Aptana IDE; Integrated Development Environment And Rails

Migrations (Chapter 23)

Here are some figures to consider while answering the following questions.

Ruby on Rails Welcome. Using the exercise files

CS169.1x Lecture 6: Basic Rails" Fall 2012"

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Laravel

Ruby on Rails. SITC Workshop Series American University of Nigeria FALL 2017

Client Side MVC with Backbone & Rails. Tom

CSE 454 Final Report TasteCliq

Ruby on Rails TKK, Otto Hilska

Web Technologies VU ( ) Vedran Sabol. Nov 13, ISDS, TU Graz. Vedran Sabol (ISDS, TU Graz) Web Technologies Nov 13, / 60

Ruby on Rails 3. Robert Crida Stuart Corbishley. Clue Technologies

Hello, world! 3.1. Ruby on Rails Web SimpleGreeter Hello, world! Rails SimpleGreeter Web Rails projects. ruby $ mkdir -p ~/projects

Introduction to Ruby on Rails

Web Frameworks MMIS 2 VU SS Denis Helic. March 10, KMI, TU Graz. Denis Helic (KMI, TU Graz) Web Frameworks March 10, / 18

User Authentication and Session Control

Tomasz Szumlak WFiIS AGH 23/10/2017, Kraków

COM401 Software Engineering Laboratory

Introduction to Ruby on Rails

Front End Programming

5/19/2015. Objectives. JavaScript, Sixth Edition. Introduction to the World Wide Web (cont d.) Introduction to the World Wide Web

Model-View-Controller (MVC)

20486-Developing ASP.NET MVC 4 Web Applications

Day 8: 7/June/2012. Log-in Authentication

Getting started with Convertigo Mobilizer

Introduction to Ruby on Rails

Help Me! A Consumer Product Assistance Application

Are you using Ruby on Rails?

Web System Development with Ruby on Rails

Episode 298. Getting Started With Spree

CS 498RK FALL RESTFUL APIs

COPYRIGHTED MATERIAL. Building Resources. A Good Place to Start

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. TurboGears

Shankersinh Vaghela Bapu Institue of Technology

Screening applicants of SIIT scholarship

Assignment 2. Start: 15 October 2010 End: 29 October 2010 VSWOT. Server. Spot1 Spot2 Spot3 Spot4. WS-* Spots

Early Data Analyzer Web User Guide

1. Setup a root folder for the website 2. Create a wireframe 3. Add content 4. Create hyperlinks between pages and to external websites

20486: Developing ASP.NET MVC 4 Web Applications (5 Days)

Instructions for the Day of Ruby Ruby on Rails Tutorial

20486: Developing ASP.NET MVC 4 Web Applications

Getting Started with Rails

Web System Development by Ruby on Rails. Day 1(20/Sept/2012) Guidance Installation of Ruby, Gems, Rails, and Aptana

Oracle Service Cloud Integration for Develope

Creating an Online Catalogue Search for CD Collection with AJAX, XML, and PHP Using a Relational Database Server on WAMP/LAMP Server

CS 155 Project 2. Overview & Part A

Learn Ruby on Rails. Learn Web Development With Ruby on Rails

A Guide to Automation Services 8.5.1

OU Campus Training. Web Services Unit

Alpha College of Engineering and Technology. Question Bank

Course 20486B: Developing ASP.NET MVC 4 Web Applications

Developing ASP.NET MVC 4 Web Applications

Ruby on Rails 3 March 14th, 2011 Ken Li Allison Pon Kyra Leimert Matt Delaney Edward Bassett

CSC 405 Computer Security. Web Security

welcome to BOILERCAMP HOW TO WEB DEV

ASP.NET MVC Training

ConJobs: Part 1 - Program your receipt printer

Interlink Express Desktop Printing Service Installation Guide

Google Docs Handout. Carol LaRow

The Sky s the Limit. JAOO Conference, Aarhus Ernest Micklei, QNH PhilemonWorks Tim Matthews, Cincom Systems. Cincom EMEA Central

With Google documents, you can easily create, share, and edit documents online.

AngularJS. CRUD Application example with AngularJS and Rails 4. Slides By: Jonathan McCarthy

Oracle Service Cloud Integration for Developers Ed 1

Developing ASP.Net MVC 4 Web Application

MVC 4 Setup and Configuration Training Document. Prepared by Andre Masters. Rev 1.0

COURSE 20486B: DEVELOPING ASP.NET MVC 4 WEB APPLICATIONS

Application Development in Web Mapping 6.

CS Final Exam Review Suggestions - Spring 2018

Kendo UI. Builder by Progress : Using Kendo UI Designer

Contents in Detail. Foreword by Xavier Noria

Rails Routing Roundup

Website Creating Content

Application Development in Web Mapping 6.

Developing ASP.NET MVC 4 Web Applications

Working with Controllers

Bringing Together One ASP.NET

Oracle Service Cloud Integration for Developers Ed 1

AngularJS. CRUD Application example with AngularJS and Rails 4. Slides By: Jonathan McCarthy


Version 1 test 11.46am. Drupal Training Manual

Web Development & SEO (Summer Training Program) 4 Weeks/30 Days

Fiz: A Component Framework for Web Applications. John Ousterhout Stanford University

1 Setting Up Your Auto Login Link in Windows

Student Instructions SD# /16 Awards Program

How to: Create a Site in a SharePoint Site Collection. Updated: 12 July 2012

FLIPBOOK CREATOR FOR IPHONE Realistic book reading experience on ipad

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

Transcription:

CSCI-2320 Web Programming: Ruby on Rails Mohammad T. Irfan Plan u Model-View-Controller (MVC) framework of web programming u Ruby on Rails 1

Ruby on Rails u Developed by David Hansson released 2004 u MVC architecture u MVC by Trygve Reenskaug, 1979 u GUI for Smalltalk u Learning Resources u Quick guide http://guides.rubyonrails.org/ getting_started.html u Best online book https://www.railstutorial.org/book Interview of David H. Hansson u Ruby Is Closer to Human Thought Than to Code u http://bigthink.com/users/davidheinemeierhansson 2

Ruby on Rails MVC framework u Goal u Decouple the three parts of an application u Model u Database u Constraints on data u Object Relational Mapping (ORM) u Maps tables to classes, rows to objects u Called ActiveRecord Ruby on Rails MVC framework u View u Prepares and presents results for users u Templates u XHTML u XML u Javascript 3

Ruby on Rails MVC framework u Controller u Takes user input u Consults with model u Directs the view u The basic codes are auto-generated Getting started u New web application (Aptana Studio 3) u Create a new Rails project u Or, from the terminal inside Aptana Studio 3 u rails new projectname u Windows users: open Gemlock.lock file, change sqlite 3 (1.3.8-...) to sqlite 3 (1.3.8) u Start the server u Command (you may use Aptana terminal) u rails server u Open a browser and go to http://localhost:3000/ u Welcome aboard 4

Browse the project folders u App u models u views u controllers Scaffolding u Fast process of generating start-up codes u First, design a schema bid name email B01224 Bob bob@bowdoin.edu.................. students u Command for ORM u rails generate scaffold Student bid:string name:string email:string u Other useful commands: rails destroy scaffold... (delete a previous ORM) 5

Migrate model to DB u Command for migration u bundle exec rake db:migrate u Reverse is: rake db:rollback (don t run it now) u rake u Ruby s make u configure, make, make install View the webpage u Command u rails s u s is shortcut for server u Go to http://localhost:3000 on web browser u No surprise there 6

Surprise! u Navigate to http://localhost:3000/students What s going on? 1. Chrome: http://localhost:3000/students 2. <Ruby Router> routes to students_controller.rb 3. students_controller.rb gets data from database table students 4. students_controller.rb feeds data to View<index.html.erb> (erb = embedded Ruby) 5. index.html.erb produces a nice html file and gives it to students_controller.rb 6. students_controller.rb gives that html file to Chrome 7

Rails router u config/routes.rb u resources :students u Routes to app/controllers/ students_controller.rb u class StudentsController < ApplicationController...... end # GET /students/new def new @student = Student.new end models/ student.rb Rails architecture u Representational State Transfer (REST) u Roy Fielding (2000) architectural style u Clients communicate with web service u Limited number of verbs u Resources (nouns) identified by URI u Rails u Nouns: objects (tables) in ORM u Verbs: Read, create, update, delete u HTTP u Nouns: URL u Verbs: GET, POST, PATCH, DELETE 8

Creating a new website 1. With its own controller 2. Without its own controller Website with dedicated controller u Command u rails generate controller MyHomePage home contact --no-test-framework u Controller class u Views 9

Navigating to my_home_page/home... u The home method of the Controller class is executed first u Empty for now u Then the corresponding view is executed u home.html.erb u You may edit it as you like Adding a page without adding new controller u First, modify config/route.rb u get my_home_page/projects u Modify the controller class in my_home_page_controller.rb u u def projects end u Create view u Add a new projects.html.erb file in views/ my_home_page folder Put it in app/assets/images u Any content: <h1>here are my Ruby projects</h1> <%= image_tag "Ruby_logo.png"%> More here: http://guides.rubyonrails.org/ layouts_and_rendering.html 10

Building an auction app from scratch without scaffolding Plan u Rails web application u A more involved example u Without scaffolding u Understand flow of control u Problem: web service with database connectivity auction u Input: name and bid amount u Store bid information in database u Output: show all bids in sorted order 11

Google this picture Download it (Copy to assets/images later) First Step u Create an application u rails new AuctionApp u Create a controller the only controller u rails generate controller AuctionApp index 12

Routing config/routes.rb u Make the index page the root (http://localhost:3000) u root auction_app#index u Other routing information (previously these were done automatically when you said resources :students) u get "/auction_app" => auction_app#index u get "/" => auction_app#index u post "/" => auction_app#enterbid Start the server u Open 2 terminals one for server, one for other commands u In both terminals, you must cd to the appropriate project folder in terminal in my case it s the AuctionApp folder u Command to start server: rails s 13

Model without scaffolding u Create a model: ORM u rails generate model Bid bidder:string amount:float u Create actual database table u bundle exec rake db:migrate #creates DB table bids u Suppose we don t want a separate controller for this (want to use auction_app_controller) u Don t say resources :bids in routes.rb u If you say so, it will automatically (without writing it explicitly in routes.rb) map HTTP get, post, etc. to index, create, etc. methods of the bids_controller.rb which we don t have! Controller u Action for the Enter Bid button u auction_app/enterbid: enterbid method in auction_app_controller u Next: write this method u This is the method that will be called when the submit button is pressed u You are allowed to pick any name for the method u Must match with the router though! 14

Controller Method name Argument: responder obj. Body More: http://bit.ly/1p1l8ar Other DB functions u newrow.save u newrow.update u newrow.destroy u Bid.find(map) 15

View Create the view: HTML way or ERB way Your image name could be different! View (continued) 16

To see the actual database files: 1. cd to db folder 2. command: sqlite3 development.sqlite3 >.tables > select * from bids; Flow of control u localhost:3000 è routes.rb routes it to auction_app_controller s index method è shows output of index.html.erb u Enter data in form and press Enter Bid button è routes.rb routes it auction_app_controller s enterbid method (why not the index method?) è Redirects to homepage 17

Assignment on Rails Groups of 2 students (collaborate & share, but type your own code) Individual submission required u Create a Ruby-on-Rails project u Open-ended u Most basic requirements u Take user input u Process that input u Work with database u Show some output u Work with non-textual data u Use other gems u Present to prof during Final Exam time Multiple Forms (One Controller) 18

Auction App u Create new button to find the leader 1. View: add embedded Ruby (erb) code for new form [alternative: HTML] 2. routes.rb: Enter the name of a new method to handle multiple posts u u One post for entering bids Another for finding leader 3. Controller: New post-handler method and new method for finding the leader View (index.html.erb) 19

routes.rb Next: add methods to the controller class 20

Click Message from the Rails Server: note how post is handled 21