11/19/14 ¡ 1 ¡
CSCI-2325 Web Programming: Ruby on Rails
Mohammad T . Irfan
Plan
u Model-View-Controller (MVC) framework of
web programming
u Ruby on Rails
CSCI-2325 Web Programming: Ruby on Rails Mohammad T . Irfan Plan - - PDF document
11/19/14 CSCI-2325 Web Programming: Ruby on Rails Mohammad T . Irfan Plan u Model-View-Controller (MVC) framework of web programming u Ruby on Rails 1 11/19/14 Ruby on Rails u Developed by David Hansson released
u Model-View-Controller (MVC) framework of
u 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
http://ruby.railstutorial.org/chapters/beginning
u “Ruby Is Closer to Human Thought Than to Code”
u http://bigthink.com/users/davidheinemeierhansson
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
u View
u Prepares and presents results for users u Templates
u XHTML u XML u Javascript
u Controller
u Takes user input u Consults with model u Directs the view
u The basic codes are auto-generated
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
u Welcome aboard
u App
u models u views u controllers
u Fast process of generating start-up codes u First, design a schema 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)
bID name email B01224 Bob bob@bowdoin.edu ... ... ... ... ... ... students
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
u Command
u rails s
u s is shortcut for server
u Go to http://localhost:3000 on web browser
u No surprise there
u Navigate to http://localhost:3000/students
u config/routes.rb
u resources :students
u Routes to app/controllers/
u class StudentsController <
def new @student = Student.new end ... end
models/ student.rb
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
u Command
u rails generate controller MyHomePage home
contact --no-test-framework u Controller class u Views
u The home method of the Controller class is
u Empty for now
u Then the corresponding view is executed
u home.html.erb u You may edit it as you like
u First, modify config/route.rb
u get “my_home_page/projects”
u Modify the controller class in
u def projects u end
u Create view
u Add a new projects.html.erb file in views/
my_home_page folder u Any content: <h1>Here are my Ruby projects</h1> <%= image_tag "Ruby_logo.png"%>
Put it in app/assets/images
More here: http://guides.rubyonrails.org/ layouts_and_rendering.html
u Is there a final exam?
u No
u Is there a big final project?
u Already done
u Future assignments
u Rails (Due 12/3) u Haskell (Due 12/10) u Prolog (Due 12/19)
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 Using other gems is strongly recommended
u Ruby vs. Rails
u Rails web application
u A more involved example u Without scaffolding u Understand flow of control
u Problem: web service with database
u Input: name and bid amount u Store bid information in database u Output: show all bids in sorted order
Google this picture Download it (Copy to assets later)
u Create an application
u rails new AuctionApp
u Create a controller – the only controller
u rails generate controller AuctionApp index
u Make the index page the root (http://localhost:3000)
u root “auction_app#index”
u Other routing information (previously these were done
u get "/auction_app" => “auction_app#index” u get "/" => “auction_app#index” u post "/" => “auction_app#enterBid”
u You must cd to the appropriate project
u rails s
u Open a different terminal for the rest of the
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
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!
Create the view: HTML way or ERB way
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
Method name Argument: responder obj. Body
More: http://bit.ly/1p1L8AR
u newRow.save u newRow.update u newRow.destroy u Bid.find(map)
bids table in sqlite3
u localhost:3000
u Enter data in form and press “Enter Bid”