jicc teaching rails
TRANSCRIPT
![Page 1: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/1.jpg)
Rails provides the structure for disciplined web scripting, or
Bruce Scharlau
Computing Science Department
University of Aberdeen
Cannelloni beats spaghettiCannelloni beats spaghetti
![Page 2: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/2.jpg)
There are different languages to choose for web programming
PythonPythonPHPPHP RubyRuby
VBVB JavaJavaC#C#
interpretedinterpreted
compiledcompiled
![Page 3: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/3.jpg)
Picking a language to teach for web scripting can be difficult
versus
Familiar and availableFamiliar and available
unknownunknown
![Page 4: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/4.jpg)
Some scripting languages don’t encourage good habitsdemo/
scripts/connect.txt
functions.txt
index.php
list.php
menu.txt
<?phpinclude("scripts/connect.txt");include(“scripts/functions.txt”);$query = "SELECT * FROM auctionItems “.“where id = '" .$id ."'" ;$result = @mysql_query($query);include(“scripts/menu.txt”);while($row=mysql_fetch_array($result)) {echo("<p>Description: ". $row["description"] .“ ");echo("Current Price: " .$row["highBid"] ."</p>");?>
Spaghetti code = code from many files
Spaghetti code = code from many files
![Page 5: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/5.jpg)
Ruby on Rails provides structured web scriptingtravelagent/
app/
controllers/
client_controller.rb
models/
show.rhtml
class ClientController < ApplicationController def show @customer = Customer.find(params[:id]) endend
Cannelloni code = self contained code
Cannelloni code = self contained code
class Customer < ActiveRecord::Baseend
views/
customer.rb<% for column in Customer.content_columns %> <p><b><%= column.human_name %>: </b> <%=h @customer.send(column.name) %></p><% end %>
client/
![Page 6: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/6.jpg)
Learn and teach Ruby on Rails for web scripting
Cannelloni beats spaghettiCannelloni beats spaghetti
![Page 7: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/7.jpg)
Teach how Rails implements the model-view-controller pattern
ControllerController
ModelModel
ViewView
![Page 8: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/8.jpg)
Controllers determine the action
ControllerControllerBrowserBrowser
![Page 9: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/9.jpg)
Models handle the data
ControllerController
DatabaseDatabase
ModelModel
BrowserBrowser
![Page 10: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/10.jpg)
Views serve the presentation
ControllerController
DatabaseDatabase
ModelModel
ViewView
BrowserBrowser
![Page 11: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/11.jpg)
Teach Ruby along with Rails
RubyRuby RailsRails
![Page 12: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/12.jpg)
Teach how to create instances and classes
class ClientController < ApplicationController… def new @customer = Customer.new end …end
![Page 13: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/13.jpg)
Teach to iterate through arrays
<p><% @customer.creditcards.each do |
cc| %><%= cc.number %><%= cc.name_on_card %><%= cc.organization %><%= cc.exp_date %></p> <% end %>
![Page 14: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/14.jpg)
Teach exception handling, helpers and filters
def show @cruise = Cruise.find(params[:id]) rescue logger.error("Attempt to access
invalid cruise #{params[:id]}") flash[:notice2] = 'Invalid cruise' redirect_to(:action=>'list')
end
![Page 15: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/15.jpg)
Use a flexible and familiar example
![Page 16: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/16.jpg)
Start with simple components
ruby script/generate scaffold -s Customer Client
![Page 17: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/17.jpg)
Integrate object/relationship mapping for more complexity
class Creditcard < ActiveRecord::Base belongs_to :customerend
class Customer < ActiveRecord::Base has_one :address has_many :creditcards has_many :reservationsend
![Page 18: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/18.jpg)
Provide a foundation for more elaborate examples
<%= link_to_remote("Do the Ajax thing", :update => 'mydiv', :url => { :action => :say_hello }) %>
![Page 19: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/19.jpg)
Is there a good structure for teaching web scripting?
Cannelloni or spaghetti?Cannelloni or spaghetti?
![Page 20: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/20.jpg)
Ease of scripting threatens disciplined scripting
Everybody thinks they know how to handle spaghetti
Everybody thinks they know how to handle spaghetti
![Page 21: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/21.jpg)
Teach Ruby on Rails for web scripting
Cannelloni beats spaghettiCannelloni beats spaghetti
![Page 22: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/22.jpg)
Rails provides the structure for disciplined web scriptingtravelagent/
app/
controllers/
client_controller.rb
models/
show.rhtml
class ClientController < ApplicationController def show @customer = Customer.find(params[:id]) endend
Cannelloni code = self contained code
Cannelloni code = self contained code
class Customer < ActiveRecord::Baseend
views/
customer.rb<% for column in Customer.content_columns %> <p><b><%= column.human_name %>: </b> <%=h @customer.send(column.name) %></p><% end %>
client/
![Page 23: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/23.jpg)
Ruby on Rails: Structure for web scripting
Filled cannelloni beats saucy spaghetti
Filled cannelloni beats saucy spaghetti
![Page 24: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/24.jpg)
Demo
1. command 'rails demo'2. command 'cd demo'3. open database.yml and change details to travelagent db4. command 'ruby script/generate scaffold customer client'5. start server with command 'ruby script/server'6. open browser to localhost:3000/client7. run through methods, edit, show, delete8. open phpmyadmin and add column 'paid' to customer table9. Re-run scaffold command10.Add 'hello' method to 'client_controller':
def hello @time = Time.now end
11.copy view/client/show.rhtml -> hello.rhtml and add code‘And the time is: %[email protected]_s%’
![Page 25: Jicc teaching rails](https://reader035.vdocument.in/reader035/viewer/2022070316/5559309fd8b42a543d8b48f7/html5/thumbnails/25.jpg)
Resources:
Bruce Scharlau
http://www.csd.abdn.ac.uk/~bscharla/
Department of Computer Science
University of Aberdeen
Bruce Scharlau
http://www.csd.abdn.ac.uk/~bscharla/
Department of Computer Science
University of Aberdeen
The book: Agile Web Development with Rails, 2nd Edition,by Dave Thomas, and David H. Hanssonhttp://www.pragmaticprogrammer.com/titles/rails/The site: http://www.rubyonrails.com/The editor: http://www.radrails.org/