Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ruby on Rails on Oracle 2009.5.20 Hoon Lee Department of Logistics Information Technology 목차 0. Overview 1. Oracle 11g Oracle.com, Installation, Registration, … 2. Ruby in Twenty Minutes IRB, Module, Method, String, Class, … 3. Ruby on Rails rubyonrails.org, Download, Installation, … 4. Ruby on Rails on Oracle Setup, Install, Create, Edit, Browser, … 5. Another way using Oracle Create, Edit, Script, Browser, … 6. Reference 2 0. Overview Who made it?, What does it? – – – – Ruby has made by Yukihiro Matsumoto on 1993. Ruby is object oriented script language. (ex: Perl, Python, PHP) Rails has made by David Heinemeier Hansson in 37signals. Rails has been developed to Web Application Development Framework by using Ruby language. Yukihiro Matsumoto David Heinemeier Hansson 3 1. Oracle 11g Oracle.com 4 1. Oracle 11g Installation Mode/Type Click to next... 5 1. Oracle 11g Pre-conditions Click to next... 6 1. Oracle 11g Registration of Oracle Configuration Manager Click to next... 7 1. Oracle 11g Summary for Installation Click to next... 8 1. Oracle 11g Installation 9 1. Oracle 11g Setup of Configuration 10 1. Oracle 11g Result of Configuration Click to next... 11 1. Oracle 11g Password Management 12 1. Oracle 11g End of Installation Click to next... 13 1. Oracle 11g Installed Programs and Environment 14 1. Oracle 11g Database Control 15 1. Oracle 11g Database Control – Instance Information 16 1. Oracle 11g SQL Plus – DBA_USERS 17 1. Oracle 11g Database Control – Unlock SCOTT 18 1. Oracle 11g Database Control – No/Yes? 19 1. Oracle 11g SQL Plus – Connection to SCOTT SQL> update dba_users set lock_date = NULL where username = ‘SCOTT’; Before the change on Oracle Control After the change on Oracle Control 20 1. Oracle 11g SQL Plus – Connection to SCOTT 21 1. Oracle 11g Environment for RoR – tnsname.ora, listener.ora LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 211.212.234.18)(PORT = 1521)) ) ) listener.ora Sample programs use a tnsname(RAILS) in Ruby on Rails. tnsname.ora 22 1. Oracle 11g Tip - “Microsoft LoopBack Adapter” – IP: 192.168.0.1 -> tnsname.ora -> listener.ora Oracle 11g doesn’t support localhost(127.0.0.1) 23 1. Oracle 11g Tip – OracleDBConsoleorcl – Start Type: Manual Oracle 11g has a bug with OracleDBConsoleorcl 24 2. Ruby in Twenty Minutes www.ruby-lang.com 25 2. Ruby in Twenty Minutes http://tryruby.hobix.com/ 26 2. Ruby in Twenty Minutes http://www.rubylang.org/en/documentation/quickstart/ 27 2. Ruby in Twenty Minutes Interactive Ruby – IRB: stands for Interactive Ruby irb(main):001:0> "Hello World" => "Hello World" irb(main):002:0> 28 2. Ruby in Twenty Minutes Ruby Obeyed You! – to print out “Hello World” – puts is the basic command to print something – puts always returns nil(absolutely-positively-nothing value) irb(main):002:0> puts "Hello World" Hello World => nil irb(main):003:0> Your Free Calculator is Here – Basic calculate, Math objects irb(main):003:0> => 5 irb(main):004:0> => 6 irb(main):005:0> => 9 irb(main):006:0> => 3.0 irb(main):007:0> 3+2 3*2 3**2 Math.sqrt(9) 29 2. Ruby in Twenty Minutes – Modules, Group Code by Topic – Modules serve two roles in Ruby. – One role = grouping similar methods together under a familiar name – Method with a parameter irb(main):007:0> a = 3 ** 2 => 9 irb(main):008:0> b = 4 ** 2 => 16 irb(main):009:0> Math.sqrt(a+b) => 5.0 irb(main):010:0> Definition of the method irb(main):010:0> def h irb(main):011:1> puts "Hello World!" irb(main):012:1> end => nil irb(main):013:0> • nil tells us that it knows we’re done defining the method 30 2. Ruby in Twenty Minutes The Brief, Repetitive Lives of a Method – Calling a method irb(main):013:0> h Hello World! => nil irb(main):014:0> h() Hello World! => nil irb(main):015:0> • If method doesn’t take parameters, empty parentheses aren't needed – Method with parameter irb(main):015:0> irb(main):016:1> irb(main):017:1> => nil irb(main):018:0> Hello Matz! => nil irb(main):019:0> def h(name) puts "Hello #{name}!" end h("Matz") • The braces(“#{name}”) is turned into a string 31 2. Ruby in Twenty Minutes Holding Spots in a String – Method with default parameter irb(main):019:0> irb(main):020:1> irb(main):021:1> => nil irb(main):022:0> Hello Chris! => nil irb(main):023:0> Hello World! => nil def h(name = "World") puts "Hello #{name.capitalize}!" end h "chris" h • The parentheses are optional on #023 • The braces(“#{name}”) is capitalized properly 32 2. Ruby in Twenty Minutes Evolving Into a Greeter – “Greeter” class with methods irb(main):024:0> class Greeter irb(main):025:1> def initialize(name = "World") irb(main):026:2> @name = name irb(main):027:2> end irb(main):028:1> def say_hi irb(main):029:2> puts "Hi #{@name}!" irb(main):030:2> end irb(main):031:1> def say_bye irb(main):032:2> puts "Bye #{@name}, come back soon." irb(main):033:2> end irb(main):034:1> end => nil • @name is an instance variable, is a available to all the methods in the class 33 2. Ruby in Twenty Minutes irb(main):035:0> g = Greeter.new("Pat") => #<Greeter:0x16cac @name="Pat"> irb(main):036:0> g.say_hi Hi Pat !=> nil irb(main):037:0> g.say_bye Bye Pat, come back soon. => nil • Create a greeter object g. it’s name is with “Pat”. rb(main):038:0> g.@name SyntaxError: compile error (irb):52: syntax error, unexpected tIVAR g.@name ^ from (irb):52 • If we want to get at the name directly? Can’t do it. • Instance variables are hidden away inside the object. 34 2. Ruby in Twenty Minutes Under the Object’s Skin – The hidden methods exist in Greeter objects? irb(main):039:0> Greeter.instance_methods => ["inspect", "clone", "to_yaml", "public_methods", "display", "instance_variab le_defined?", "equal?", "freeze", "methods", "respond_to?", "dup", "instance_var iables", "__id__", "method", "eql?", "h", "id", "to_yaml_properties", "singleton _methods", "taguri", "send", "taint", "frozen?", "taguri=", "instance_variable_g et", "say_hi", "__send__", "instance_of?", "to_a", "type", "to_yaml_style", "pro tected_methods", "object_id", "instance_eval", "==", "===", "instance_variable_s et", "say_bye", "kind_of?", "extend", "to_s", "hash", "class", "tainted?", "=~", "private_methods", "nil?", "untaint", "is_a?"] • this is all of the methods for Greeter objects, including ones defined by ancestor classes. irb(main):040:0> Greeter.instance_methods(false) => ["say_bye", "say_hi"] • to not include ancestors by passing it the parameter false 35 2. Ruby in Twenty Minutes irb(main):041:0> g.respond_to?("name") => false irb(main):042:0> g.respond_to?("say_hi") => true irb(main):043:0> g.respond_to?("to_s") => true • Which methods our greeter object responds to? • “to_s” is used to convert something to a string. Altering Classes - It’s Never Too Late – Access to an object’s variables irb(main):044:0> class Greeter irb(main):045:1> attr_accessor :name irb(main):046:1> end => nil • The changes will be present in any new objects. 36 2. Ruby in Twenty Minutes irb(main):047:0> g = Greeter.new("Andy") => #<Greeter:0x3c9b0 @name="Andy"> irb(main):048:0> g.respond_to?("name") => true irb(main):049:0> g.respond_to?("name=") => true irb(main):050:0> g.say_hi Hi Andy! => nil irb(main):051:0> g.name="Betty" => "Betty" irb(main):052:0> g => #<Greeter:0x3c9b0 @name="Betty"> irb(main):053:0> g.name => "Betty" irb(main):054:0> g.say_hi Hi Betty! => nil • Using attr_accessor defined two new methods, name to get the value, and name= to set it. 37 2. Ruby in Twenty Minutes Greeting Anything and Everything, MegaGreeter Neglects None! – Create a file, and run it. • To quit IRB, type “quit”, “exit” or just hit Control-D #!/usr/bin/env rubyclass MegaGreeter attr_accessor :names # Create the object def initialize(names = "World") @names = names end # Say hi to everybody def say_hi if @names.nil? puts "..." elsif @names.respond_to?("each") # @names is a list of some kind, iterate! @names.each do |name| puts "Hello #{name}!" end else puts "Hello #{@names}!" end end ri20min.rb38 2. Ruby in Twenty Minutes # Say bye to everybody def say_bye if @names.nil? puts "..." elsif @names.respond_to?("join") # Join the list elements with commas puts "Goodbye #{@names.join(", ")}. Come back soon!" else puts "Goodbye #{@names}. Come back soon!" end end end if __FILE__ == $0 mg = MegaGreeter.new mg.say_hi mg.say_bye # Change name to be "Zeke" mg.names = "Zeke" mg.say_hi mg.say_bye # Change the name to an array of names mg.names = ["Albert", "Brenda", "Charles", "Dave", "Englebert"] mg.say_hi mg.say_bye # Change to nil mg.names = nil mg.say_hi mg.say_bye end ri20min.rb 39 2. Ruby in Twenty Minutes • Save this file as “ri20min.rb”, and run it as “ruby ri20min.rb” Hello World! Goodbye World. Come back soon! Hello Zeke! Goodbye Zeke. Come back soon! Hello Albert! Hello Brenda! Hello Charles! Hello Dave! Hello Englebert! Goodbye Albert, Brenda, Charles, Dave, Englebert. Comeback soon! ... ... ruby ri20min.rb • Sharp mark(#) is a comment line, but first line of the file tells how to run the file. Cycling and Looping - a.k.a. Iteration @names.each do |name| puts "Hello #{name}!“ end • The variable between pipe characters is the parameter for this block(@names) 40 2. Ruby in Twenty Minutes Blocks, the Highly Sparkling Glint on the Edge of Ruby – The real power of block elsif @names.respond_to?("join") # Join the list elements with commas puts "Goodbye #{@names.join(", ")}. Come back soon!" • Elements are joined/sequenced by parameter. Prints out like string. Kicking Off the Script if __FILE__ == $0 • __FILE__ contains the name of current file. • $0 is the name of the file used to start the program • This check says “If this is the main file being used…” This allows a file to be used as a library, and not to execute code in that context, but if the file is being used as an executable, then execute that code 41 2. Ruby in Twenty Minutes Ruby From Other Languages 42 3. Ruby on Rails http://rubyonrails.org/ 43 3. Ruby on Rails Smart people saying nice things – “Rails is the most well thought-out web development framework I’ve ever used. And that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped develop the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.” - James Duncan Davidson, Creator of Tomcat and Ant – – “Ruby on Rails is a breakthrough in lowering the barriers of entry to programming. Powerful web applications that formerly might have taken weeks or months to develop can be produced in a matter of days.” - Tim O'Reilly, Founder of O'Reilly Media - 44 3. Ruby on Rails – “It is impossible not to notice Ruby on Rails. It has had a huge effect both in and outside the Ruby community... Rails has become a standard to which even well-established tools are comparing themselves to.” - Martin Fowler, Author of Refactoring, PoEAA, XP Explained – – “What sets this framework apart from all of the others is the preference for convention over configuration making applications easier to develop and understand.” - Sam Ruby, ASF board of directors - – “Rails is the killer app for Ruby.” - Yukihiro Matsumoto, Creator of Ruby - 45 3. Ruby on Rails Who is already on Rails? 46 3. Ruby on Rails Download software 47 3. Ruby on Rails RubyGems 48 3. Ruby on Rails http://rubyforge.org/ 49 3. Ruby on Rails Installation of RubyGems & Rails 50 3. Ruby on Rails Install a Rails – Run “gem install rails” if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q<rake>, [">= 0.8.3"]) s.add_runtime_dependency(%q<activesupport>, ["= 2.3.2"]) s.add_runtime_dependency(%q<activerecord>, ["= 2.3.2"]) s.add_runtime_dependency(%q<actionpack>, ["= 2.3.2"]) s.add_runtime_dependency(%q<actionmailer>, ["= 2.3.2"]) s.add_runtime_dependency(%q<activeresource>, ["= rails-2.3.2.gemspec 2.3.2"]) Ref) C:\Ruby\lib\ruby\gems\1.8\cache/doc/gems/specifications 51 3. Ruby on Rails Make a Web Application 52 3. Ruby on Rails Make a New Project – Run “rails path/to/your/new/application” 53 3. Ruby on Rails Run a Web Server – Move “cd path/to/your/new/application” – Run “ruby script/server” #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/server' server 54 3. Ruby on Rails http://localhost:3000/ 55 4. Ruby on Rails on Oracle http://www.oracle.com/technology/pub/articles/ha efel-oracle-ruby.html 56 4. Ruby on Rails on Oracle Step 1: Set up the Oracle database – Refer to the “1. Oracle 11g” – Create a user on SQL Plus – ruby/ruby SQL> GRANT CONNECT, RESOURCE TO ruby IDENTIFIED BY ruby; SQL> ALTER USER ruby DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp; SQL> EXIT Step 2: Install Ruby, RubyGems, Rails, and the Ruby/Rails Oracle library – Refer to the “3. Ruby on Rails” • Use the "One-Click Ruby Installer for Windows 1.8.6-26 Final Release“ • Follow the installation instructions 57 4. Ruby on Rails on Oracle – Update RubyGems to latest version C:\Ruby>gem update --system Updating RubyGems Updating rubygems-update Successfully installed rubygems-update-1.3.3 :0:Warning: Gem::SourceIndex#search support for String patterns is deprecated Updating RubyGems to 1.3.3 Installing RubyGems 1.3.3 Installing RubyGems Installing gem executable Removing old source_cache files Removing old RubyGems RDoc and ri Installing rubygems-1.3.3 ri into C:/Ruby/lib/ruby/gems/1.8/doc/rubygems-1.3.3/r i Installing rubygems-1.3.3 rdoc into C:/Ruby/lib/ruby/gems/1.8/doc/rubygems-1.3.3 /rdoc -----------------------------------------------------------------------------= Announce: RubyGems Release 1.3.3 … 58 4. Ruby on Rails on Oracle – Install Rails C:\Ruby>gem install rails -v 2.3.2 Successfully installed rails-2.3.2 1 gem installed Installing ri documentation for rails-2.3.2... Installing RDoc documentation for rails-2.3.2... – Install Ruby Oracle library(Ruby-oci8) C:\Ruby>gem install ruby-oci8 -v 1.0.4 Successfully installed ruby-oci8-1.0.4-x86-mswin32 1 gem installed Installing ri documentation for ruby-oci8-1.0.4-x86-mswin32... Installing RDoc documentation for ruby-oci8-1.0.4-x86-mswin32... – Install ActiveRecord Oracle enhanced adapter C:\Ruby>gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org Successfully installed activerecord-oracle-adapter-1.0.0.9250 1 gem installed Installing ri documentation for activerecord-oracle-adapter-1.0.0.9250... Installing RDoc documentation for activerecord-oracle-adapter-1.0.0.9250... 59 4. Ruby on Rails on Oracle Step 3: Create the Web Application – Create a new project using the rails command line application C:\Ruby\temp>rails comics_catalog -d oracle create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create config/locales create db create doc create lib create lib/tasks create log create public/images create public/javascripts create public/stylesheets create script/performance create test/fixtures create test/functional create test/integration create test/performance create test/unit create vendor create vendor/plugins create tmp/sessions 60 4. Ruby on Rails on Oracle – Edit database.yml using Oracle database If you use ActiveRecord Oracle enhanced adapter, specify 'oracle_enhanced' instead of 'oracle' as the adapter 61 4. Ruby on Rails on Oracle Create a Web application using the Rails scaffold C:\Ruby\temp\comics_catalog>ruby script/generate scaffold comic title:string issue:integer publisher:string exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/comics exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/comics/index.html.erb create app/views/comics/show.html.erb create app/views/comics/new.html.erb create app/views/comics/edit.html.erb create app/views/layouts/comics.html.erb create public/stylesheets/scaffold.css create issue:integer app/controllers/comics_controller.rb "title:string publisher:string" represents a table column create test/functional/comics_controller_test.rb Rails code generator created model, view, and controller Ruby code to create app/helpers/comics_helper.rb createCOMICS test/unit/helpers/comics_helper_test.rb access the table route map.resources :comics If youdependency make any changes to the database tables, you'll need to run the model app/models/ scaffoldexists command again. exists test/unit/ exists test/fixtures/ create app/models/comic.rb 62 create test/unit/comic_test.rb 4. Ruby on Rails on Oracle – Create a database C:\Ruby\temp\comics_catalog>rake db:migrate (in C:/Ruby/temp/comics_catalog) == CreateComics: migrating =================================================== -- create_table(:comics) -> 0.1100s -> 0 rows == CreateComics: migrated (0.1100s) ========================================== – Start a Web Server C:\Ruby\temp\comics_catalog>ruby script/server => Booting WEBrick => Rails 2.3.2 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2009-05-12 00:53:22] INFO WEBrick 1.3.1 can change the Ruby code at anytime without restarting [2009-05-12 00:53:22] INFO ruby 1.8.6 (2008-08-11) [i386-mswin32] INFO WEBrick::HTTPServer#start: port=3000 if we[2009-05-12 change 00:53:22] the database configuration pid=7376 file, need to restart the server 63 4. Ruby on Rails on Oracle Web Browser - List 64 4. Ruby on Rails on Oracle Web Browser – Create a new record 65 4. Ruby on Rails on Oracle Web Browser - List 66 4. Ruby on Rails on Oracle Web Browser – Edit a record 67 4. Ruby on Rails on Oracle http://ruby-oci8.rubyforge.org/en/ To get a Ruby Oracle Library 68 4. Ruby on Rails on Oracle http://github.com/rsim/oracleenhanced/tree/master To get a ActiveRecord Oracle Adapter 69 4. Ruby on Rails on Oracle Files and Directories – \comics_catalog\app • • • • models\comic.rb views\comics or layouts\*.erb controllers\*.rb helper\*.rb – \comics_catalog\config\database.yml – \comics_catalog\db\schema.rb – … Make API DOC – “C:\comics_catalog>rake doc:app” 70 5. Another way using Oracle SQL Plus – Create a user(RAIL) 71 5. Another way using Oracle Edit – Make a comics.sql to create table(s) and record(s) 72 5. Another way using Oracle SQL Plus – Run a script file 73 5. Another way using Oracle Create a New Project(comics_catalog) C:\> rails comics_catalog Move to directory(comics_catalog) C:\> cd comics_catalog Edit a database.yml in config directory – development: adapter: oci username: ruby password: ruby host: RAILS Create a Web application using the Rails scaffold C:\comics_catalog> ruby script/generate scaffold Comic – Start a Web Server C:\comics_catalog> ruby script/server – Check by Web Browser – http://localhost:3000/comics/list 74 6. Reference Ruby Programming Language – http://www.ruby-lang.org/en/ Ruby on Rails – http://rubyonrails.org/ RubyForge – Project Library – http://rubyforge.org/ Ruby Central – http://www.rubycentral.com/ Ruby on Rails on Oracle: A Simple Tutorial – http://www.oracle.com/technology/pub/articles/haefel-oracleruby.html Oracle Database Software Downloads – http://www.oracle.com/technology/software/products/database /index.html 75 Thank You!