Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A...
Transcript of Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A...
![Page 1: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/1.jpg)
Ruby On RailsJames Reynolds
![Page 2: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/2.jpg)
Today
Ruby on Rails introduction
Run Enviornments
MVC
A little Ruby
Exercises
![Page 3: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/3.jpg)
Installation
Mac OS X 10.5 will include Rails
Mac OS X 10.4 includes Ruby
Most people reinstall it anyway
From scratch
Drag and drop Locomotive
![Page 4: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/4.jpg)
Databases
Mysql
SQLite
PostgreSQL
DB2
Oracle
Firebird
SQL Server
more
![Page 5: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/5.jpg)
Webservers
Apache w/ FastCGI or Mongrel
LightTPD
WEBrick
![Page 6: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/6.jpg)
"IDE's"
TextMate and Terminal (preferred)
RadRails (Eclipse)
jEdit
Komodo
Arachno Ruby
NetBeans IDE
![Page 7: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/7.jpg)
Run Environments
Production
Development
Testing
![Page 8: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/8.jpg)
Run Environments
Production
Cached
Freeze Rails
Ship Rails with your app
etc
![Page 9: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/9.jpg)
Run Environments
Development
Reloads source files every time
Scaffold
![Page 10: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/10.jpg)
Run Environments
TestingConnect debugger to running webapp
Stop at breakpointsUnit testing
Integration testingFunctional testingDB is reloaded w/ each testMock and stub code
![Page 11: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/11.jpg)
MVC
First Described in 1979
Totally ignored in web dev
Except
WebObjects
Struts
JavaServer Faces
![Page 12: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/12.jpg)
ControllerView
User
Model-View-Controller
sees uses
queries
updates
Model
results
![Page 13: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/13.jpg)
User
How it works
Deals w/data
Receives URLactions
Produces HTML
![Page 14: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/14.jpg)
User
Controller.rb
How it works
View.rb
Model.rbsave()
performSave()showSuccess()showError()
HTML file:<form>
<button></form>
clicks submit button(sends url)
![Page 15: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/15.jpg)
RSSHTML
User
Why use it?
MySQL
Web BrowserPDF
flat fileBDB
Custom AppXML
![Page 16: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/16.jpg)
Controllers
Friendly urls
NO: http://example.com/?node=34
Yes: http://example.com/blog/view/34
Customizing URL’s easy
config/routes.rb
![Page 17: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/17.jpg)
Controllers
Default URL mapping
http://example.com/blog/view/34
controller = blog
action = view
id = 34
![Page 18: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/18.jpg)
Views
Can create html, xml, and js
Easy handling of params, flash, cookies, etc
Ajax built in and dumb simple
![Page 19: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/19.jpg)
Views - RHTML
Mixing up <% ... %> and <%= ... %> is a HUGE source of bugs
Be sure to put spaces around the tags
Not required, but is easier to read IMO
![Page 20: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/20.jpg)
A little Ruby
Symbols
Default parameter values
Named parameters
![Page 21: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/21.jpg)
Symbols
moods = { 'angry' => 'red', 'sick' => 'green', 'sad' => 'blue' }
puts "I'm feeling " + moods['sick']
moods = { :royal => 'purple', :angelic => 'white', :guilty => 'black' }
puts "I'm feeling " + moods[:royal]
![Page 22: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/22.jpg)
Named parameters
def printParams lotsaParams = {} if lotsaParams[:one] puts lotsaParams[:one] end if lotsaParams[:two] puts lotsaParams[:two] endendprintParams :one => '1', :two => '2'printParams :two => '2', :one => '1'
![Page 23: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/23.jpg)
Common Errors!Do not press return before “=>”
Wrong: printParams :one
=> '1', :two => '2'
Missing or misplaced “:” causes errors!Wrong: printParams one => '1',: two => '2'Right: printParams :one => '1', :two => '2'
Space or return between hash/array and [...]Wrong: moods [:royal] Right: moods[:royal]
![Page 24: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/24.jpg)
Try it ourselves
Make a webapp that says "Hello World" on the index page
Create the app (in Locomotive)
![Page 25: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/25.jpg)
Hello World
Create the controller (in Terminal)
![Page 26: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/26.jpg)
Hello World
Open the file "example_controller.rb" (in TextMate)
Create the index method
![Page 27: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/27.jpg)
Hello World
View in web browser!
![Page 28: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/28.jpg)
Hello World via template
Comment out "render_text" line
![Page 29: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/29.jpg)
Hello World via template
Create new view file "index.rhtml"
Right click on "views/example" dir
![Page 30: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/30.jpg)
Hello World via template
Insert HTML into "index.rhtml"
Preview in browser
![Page 31: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/31.jpg)
Request Params
Print out the request params
Add this to index.rhtml
Pay attention to <% vs <%=
<% params.each_pair do |key,value| %> <%= key.to_s %> => <%= value.to_s %><br/><% end %>
![Page 32: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/32.jpg)
Try it ourselves
Print out the request params
It looks like this
(action & controller always sent as params)
![Page 33: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/33.jpg)
CookiesChange index_controller.rb to this:
class ExampleController < ApplicationController def index cookies[:the_time] = Time.now.to_s redirect_to :action => :index2 end
def index2 render(:text => "The cookie time is #{cookies[:the_time]}") endend
![Page 34: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/34.jpg)
Make a Session
Put this in example_controller.rb
class ExampleController < ApplicationController def index session[:counter] ||= 0 session[:counter] += 1 end def reset_counter session[:counter] = 0 redirect_to :action => :index endend
![Page 35: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/35.jpg)
Make a Session
Put this in index.rhtml
You have been here <%= session[:counter]; pluralize(session[:counter], "time") %>.<br><%= link_to "Add one", :action=>:index %><br><%= link_to "Reset", :action=>:reset_counter %>
![Page 36: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/36.jpg)
Make a Session
Should look like this
![Page 37: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/37.jpg)
Flash Message
Put this in example_controller.rb
class ExampleController < ApplicationController def peek flash[:notice] = "A BOO!" redirect_to :action => :index endend
![Page 38: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/38.jpg)
Flash Message
Put this in index.rhtml
<% if flash[:notice] -%><div id="notice"><%= flash[:notice] %></div><% end -%>
<%= link_to "Peek?", :action => :peek %>
![Page 39: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/39.jpg)
Flash Message
Should look like this
![Page 40: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/40.jpg)
Use Layout
Look at web browser source code
No <head> or <body> tags
![Page 41: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/41.jpg)
Use Layout
Create new file "example.rhtml" in "layouts"
Be sure to use HTML template
![Page 42: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/42.jpg)
Use Layout
Cut the flash code out of index.rhtml
This is all that is left:
<%= link_to "Peek?", :action => :peek %>
![Page 43: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/43.jpg)
Use LayoutAdd code inside of example.rhtml’s <body>
Yield must have <%= !!!
The - in -%> will remove the next \n...</head><body> <% if flash[:notice] -%> <div id="notice"><%= flash[:notice] %></div> <% end -%> <%= yield %></body></html>
![Page 44: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/44.jpg)
Use Layout
Should behave the same, except the source code!
![Page 45: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/45.jpg)
Use LayoutALL pages will show flash[:notice]
Verify by adding a new page and change redirect_to :action => :index
To:redirect_to :action => :newpage
![Page 46: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/46.jpg)
Save Form w/ Session
Put in example_controller.rb
class ExampleController < ApplicationController def index session[:comment_list] ||= [ "Original item." ] end def add_item session[:comment_list].push params[:newitem] redirect_to( :action => :index ) endend
![Page 47: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/47.jpg)
Save Form w/ Session
Put in index.rhtml
<% form_tag( :action => :add_item ) do %> <%= text_field_tag :newitem %> <%= submit_tag "Add item" %><% end %>
<ul id="my_list"> <% session[:comment_list].reverse.each do |line| -%> <li><%= line -%></li> <% end -%></ul>
![Page 48: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/48.jpg)
Save Form w/ Session
Should look like this
If it isn’t, make sure your <%= is not <%
![Page 49: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/49.jpg)
Convert to AJAX
Change example_controller.rb
class ExampleController < ApplicationController def index session[:comment_list] ||= [ "Original item." ] end def add_item session[:comment_list].push params[:newitem] render_text "<li>#{params[:newitem]}</li>" endend
![Page 50: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/50.jpg)
Convert to AJAX
Change index.rhtml
<%= javascript_include_tag :defaults %><% form_remote_tag( :update => "my_list", :url => { :action => :add_item }, :position => "top" ) do %> <%= text_field_tag :newitem %> <%= submit_tag "Add item" %><% end %>
<ul id="my_list"> <% session[:comment_list].reverse.each do |line| -%> <li><%= line -%></li> <% end -%></ul>
![Page 51: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/51.jpg)
Convert to AJAX
How do you know it is AJAX?
The text field didn't go blank!
![Page 52: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/52.jpg)
No JavaScript
What happens if JavaScript is off?
![Page 53: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/53.jpg)
No JavaScript
That is unacceptable!
![Page 54: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/54.jpg)
No JavaScript
Change example_controller.rb
class ExampleController < ApplicationController def index session[:comment_list] ||= [ "Original item." ] end def add_item session[:comment_list].push params[:newitem] if request.xhr? render_text "<li>#{params[:newitem]}</li>" else redirect_to( :action => :index ) end endend
![Page 55: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/55.jpg)
No JavaScript
JavaScript off now works!
![Page 56: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/56.jpg)
No JavaScript
JavaScript on still uses AJAX!
![Page 57: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/57.jpg)
Use RJS file
Change example_controller.rb
class ExampleController < ApplicationController def index session[:comment_list] ||= [ "Original item." ] end def add_item session[:comment_list].push params[:newitem] redirect_to( :action => :index ) unless request.xhr? endend
![Page 58: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/58.jpg)
Use RJS file
Change index.rhtml
<%= javascript_include_tag :defaults %><% form_remote_tag( :update => "my_list", :url => { :action => :add_item }, :position => "top" do ) %> <%= text_field_tag :newitem %> <%= submit_tag "Add item" %><% end %>
<ul id="my_list"> <% session[:comment_list].reverse.each do |line| -%> <li><%= line -%></li> <% end -%></ul>
![Page 59: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/59.jpg)
Use RJS file
Create new file "add_item.rjs"
![Page 60: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/60.jpg)
Use RJS file
Put in add_item.rjs
This file is RUBY code, not JavaScript
It is converted to JavaScript by Rails
page.insert_html :top, "my_list", "<li>#{params[:newitem]}</li>"
![Page 61: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/61.jpg)
Use RJS file
Should behave exactly the same
Well, why did we do that??
Because we want "Magic"!
To get the "Magic", all <li>'s need to be numbered.
![Page 62: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/62.jpg)
Add MagicAdd id's to each item
Change index.rhtml
<%= javascript_include_tag :defaults %><% form_remote_tag( :url => { :action => :add_item }, :position => "top" ) do %> <%= text_field_tag :newitem %> <%= submit_tag "Add item" %><% end %><ul id="my_list"> <% session[:comment_list].reverse.each_with_index do |line, index| -%> <li id='<%= session[:comment_list].length-index -%>'><%=line%></li> <% end -%></ul>
![Page 63: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/63.jpg)
Add Magic
Add id's to each item
Change add_item.rjs
As one line (no returns)
page.insert_html :top, "my_list", "<li id='#{session[:comment_list].length}'>#{params[:newitem]}</li>"
![Page 64: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/64.jpg)
Add Magic
Add a line to add_item.rjs
ALL AS ONE LINE (no return before :highlight)
page.insert_html :top, "my_list", "<li id='#{session[:comment_list].length}'>#{params[:newitem]}</li>"
page[session[:comment_list].length.to_s].visual_effect :highlight, :startcolor => "#ffff00", :endcolor => "#ffffff"
![Page 65: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/65.jpg)
Add Magic
What have we got? Highlighting!
![Page 66: Ruby On Rails - University of California, San DiegoRuby on Rails introduction Run Enviornments MVC A little Ruby Exercises Installation Mac OS X 10.5 will include Rails Mac OS X 10.4](https://reader034.fdocuments.us/reader034/viewer/2022051910/5fff21c2975f4f4088194e9e/html5/thumbnails/66.jpg)
Done!
Next class:
Connecting to a database
Using migrations
Scaffold
Model relationships
Using before_filter