CS 683 Emerging Technologies Fall Semester, 2005 Doc 22 Rails Basic Action Pack & Active Record Nov 15, 2005 Copyright ©, All rights reserved. 2005 SDSU.

Slides:



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

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
CakePHP. A framework for developing applications in PHP Inspired by Ruby on Rails Follows MVC design pattern Convention over configuration –No wheel reinventing.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
CS 142 Lecture Notes: Rails Controllers and ViewsSlide 1 Simple Rails Template
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
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.
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.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 25 Rails Transactions, Sessions, Filters Nov 29, 2005 Copyright ©, All rights reserved SDSU.
CS 142 Lecture Notes: FormsSlide 1 Simple Form Value1: Value2:
XML and Ruby on Rails Jennifer Andrews LIS 531F April 25,2007.
15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 21 Rails Intro Nov 10, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 23 Rails Model Example Nov 17, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 24 Some Ruby GUI 2 Apr 27, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney,
CS 683 Emerging Technologies Fall Semester, 2005 Doc 24 Rails Database Nov 22, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 20 Sending Apr 13, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 21 Some GUI Apr 13, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
MVC in Trax.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
INTERNET APPLICATION DEVELOPMENT For More visit:
© 2003 By Default! A Free sample background from Slide 1 Week 2  Free PHP Hosting Setup  PHP Backend  Backend Security 
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
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.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Database and mySQL Week 07 Dynamic Web TCNJ Jean Chu.
CAKEPHP Blog tutorial. what you’ll need examples/blog/blog.html 2  A running web server  A database server.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
Chapter 17 Creating a Database.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Pertemuan 10 Enterprise Application Patterns Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
MVC Concepts Basics Model-View-Controller (MVC) Concepts for Web Developers SoftUni Team Technical Trainers Software University
CS 580 Client-Server Programming Spring Semester, 2006 Doc 16 Some Parsing Mar 30, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 19 Ruby Regexp, Expression, Exceptions, Modules Nov 8, 2005 Copyright ©, All rights reserved
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Creating A Database Driven Website 1.Setting Up Your Web Server 2.Creating a Database 3.Creating a Webpage to Display Information From a Database 4.Creating.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 26 Rails Miscellaneous Dec 1, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CHAPTER 10 PHP MySQL Database
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
Web Application Design. Data –What data is available? –How do we store it or how is it stored in the DB? Schema Data types Etc. –Where is the data?
The Controller Carol Wolf Computer Science. Rails generate commands  Using the generate command, you can create a number of useful objects.  Rails:
DAY 14: ACCESS CHAPTER 1 RAHUL KAVI October 8,
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
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.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Chapter 5 Introduction to SQL.
CS320 Web and Internet Programming SQL and MySQL
MVC Architecture, Symfony Framework for PHP Web Apps
Introduction to PHP and MySQL – Creating Database-Driven Websites
CMPE 280 Web UI Design and Development October 24 Class Meeting
Ruby on Rails Model of MVC
Ruby on Rails by Manik Juneja
Agile Web Development with Ruby and Rails
Ruby on Rails by Manik Juneja
Ajax on Rails 28-Nov-18.
Model – View – Controller Pattern
Chapter 15 Introduction to Rails.
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

CS 683 Emerging Technologies Fall Semester, 2005 Doc 22 Rails Basic Action Pack & Active Record Nov 15, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA USA. OpenContent ( license defines the copyright on this document.

2 References Agile Web Development with Rails, Thomas & Hanson, The Pragmatic Bookshelf, 2005 Patterns of Enterprise Application Architecture, Fowler, Addison-Wesley, 2003 Action Pack Documentation, Active Record Documentation,

3 Rails Framework Action Pack Routes requests Controllers Views Action Record Database access Action Mailer access

4 Action Pack Simple Request Flow

5 render :template class SayController < ApplicationController def = Time.now render :template => "say/goodbye" end def goodbye end

6 render :action class SayController < ApplicationController def = Time.now render :action => "goodbye" end def goodbye end

7 Direct Call 5 class SayController < ApplicationController def = Time.now goodbye end def goodbye end

8 render :text class SayController < ApplicationController def = Time.now render :text => "Hello There" end def goodbye end

9 redirect :action 5 class SayController < ApplicationController def = Time.now redirect_to :action => "goodbye" end def goodbye end

10 Request Headers Request Headers Header Value class SayController < ApplicationController def requestHeaderInController remote_host = request.env['REMOTE_HOST'] end def requestHeaders end app/views/say/requestHeaders.rhtml

11 Post & Get Parameters Parameters Key Value class SayController < ApplicationController def parameters a = params["a"] end end app/views/say/parameters.rhtml

12 flash - Temporary variables to View class FlashController < ApplicationController def one flash[:a] = "Hello1" end def two flash[:b] = "Hello2" end def three flash[:c] = "Hello3" end flash[:a] Header Value flash[:b] Header Value app/views/flash/one.rhtmlapp/controllers/flash_controller.rb app/views/flash/two.rhtml flash variable is flushed at the end of the subsequent action

13 flash - Example Header Value a Hello1 Header Value a Hello1 b Hello2 Header Value b Hello2 c Hello3 Header Value c Hello3 class FlashController < ApplicationController def one flash[:a] = "Hello1" end def two flash[:b] = "Hello2" end def three flash[:c] = "Hello3" end app/controllers/flash_controller.rb

14 flash now & keep class FlashController < ApplicationController def one flash[:a] = "Hello1" end def two flash.now[:b] = "Hello2" flash.keep(:a) end def three flash[:c] = "Hello3" end app/controllers/flash_controller.rb Header Value a Hello1 Header Value a Hello1 b Hello2 Header Value a Hello1 c Hello3 Header Value c Hello3

15 Layouts Header Value Header Value app/views/layouts/flash.rhtmlapp/views/flash/one.rhtml app/views/flash/two.rhtml

16 Non-default Layouts class FlashController < ApplicationController layout 'standard' #... end class FlashController < ApplicationController layout 'my_layout' #... end class FlashController < ApplicationController layout 'my_layout', :except => [:rss, :atom] def rss render(:layout => false) #no layout end class FlashController < ApplicationController layout :different_layout def different_layout > 0 "positive" else "negative" end end class FlashController < ApplicationController def one flash[:a] = "Hello1" render :layout => "layouts/anotherLayout" end

17 Partials 14 One 'flashTable' %> Two 'flashTable' %> app/views/layouts/flash.rhtml app/views/flash/one.rhtml app/views/flash/two.rhtml Header Value app/views/flash/_flashTable.rhtml class FlashController < ApplicationController def one flash[:a] = "Hello1" end def two flash.[:b] = "Hello2" end def three flash[:c] = "Hello3" end app/controllers/flash_controller.rb

18 Active Record An Object that wraps a row in a database table or view, encapsulates the database access, adds domain logic on that data "Martin Fowler" require "rubygems" require_gem "activerecord" #Connect ActiveRecord::Base.establish_connection( :adapter => "mysql", :host => "localhost", :database => "cs683BookStore_development", :username => "whitney") class Book < ActiveRecord::Base end #Add to database ruby = Book.new ruby.title = 'Programming Ruby' ruby.author = 'Thomas, Dave' ruby.date = Time.now ruby.save from_database = Book.find_by_title('Programming Ruby') puts from_database.author CREATE TABLE `books` ( `id` int(11) NOT NULL auto_increment, `title` varchar(100) NOT NULL default '', `author` varchar(50) NOT NULL default '', `date` date default ' ', PRIMARY KEY (`id`) )

19 Adding to Database ruby = Book.new ruby.title = 'Programming Ruby' ruby.author = 'Thomas, Dave' ruby.date = Time.now ruby.save aBook = Book.new( :title => 'Programming Ruby', :author => 'Thomas, Dave', :date => Time.now ) aBook.save aBook = Book.create( :title => 'Programming Ruby', :author => 'Thomas, Dave', :date => Time.now ) params = { :title => 'Programming Ruby', :author => 'Thomas, Dave', :date => Time.now} aBook = Book.create(params) a = Book.find_by_title('Programming Ruby') a = Book.find_by_author('Thomas, Dave') a = Book.find_all_by_author('Thomas, Dave') a = Book.find_by_sql("select * from Books where author = 'Thomas, Dave' ") a = Book.find(:first, :conditions => "author = 'Thomas, Dave'") a = Book.find(:first, :conditions => [ "author = 'Thomas, Dave' and title = 'Programming Ruby'"]) a = Book.find(:all, :conditions => [ "author = 'Thomas, Dave'"]) name = 'Thomas, Dave' a = Book.find(:first, :conditions => [ "author = ?", name])

20 Reading from the Database a = Book.find_by_title('Programming Ruby') a = Book.find_by_author('Thomas, Dave') a = Book.find_all_by_author('Thomas, Dave') a = Book.find_by_sql("select * from Books where author = 'Thomas, Dave' ") a = Book.find(:first, :conditions => "author = 'Thomas, Dave'") a = Book.find(:first, :conditions => [ "author = 'Thomas, Dave' and title = 'Programming Ruby'"]) a = Book.find(:all, :conditions => [ "author = 'Thomas, Dave'"]) name = 'Thomas, Dave' a = Book.find(:first, :conditions => [ "author = ?", name])

21 Modifying & Deleting a = Book.find_by_title('Programming Ruby') a.author = 'Dave Thomas' a.save Book.find_by_author('Dave Thomas').destroy