Upload
bernard-loire
View
567
Download
0
Embed Size (px)
Citation preview
Rails provides the structure for disciplined web scripting, or
Bruce Scharlau
Computing Science Department
University of Aberdeen
Cannelloni beats spaghettiCannelloni beats spaghetti
There are different languages to choose for web programming
PythonPythonPHPPHP RubyRuby
VBVB JavaJavaC#C#
interpretedinterpreted
compiledcompiled
Picking a language to teach for web scripting can be difficult
versus
Familiar and availableFamiliar and available
unknownunknown
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
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/
Learn and teach Ruby on Rails for web scripting
Cannelloni beats spaghettiCannelloni beats spaghetti
Teach how Rails implements the model-view-controller pattern
ControllerController
ModelModel
ViewView
Controllers determine the action
ControllerControllerBrowserBrowser
Models handle the data
ControllerController
DatabaseDatabase
ModelModel
BrowserBrowser
Views serve the presentation
ControllerController
DatabaseDatabase
ModelModel
ViewView
BrowserBrowser
Teach Ruby along with Rails
RubyRuby RailsRails
Teach how to create instances and classes
class ClientController < ApplicationController… def new @customer = Customer.new end …end
Teach to iterate through arrays
<p><% @customer.creditcards.each do |
cc| %><%= cc.number %><%= cc.name_on_card %><%= cc.organization %><%= cc.exp_date %></p> <% end %>
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
Use a flexible and familiar example
Start with simple components
ruby script/generate scaffold -s Customer Client
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
Provide a foundation for more elaborate examples
<%= link_to_remote("Do the Ajax thing", :update => 'mydiv', :url => { :action => :say_hello }) %>
Is there a good structure for teaching web scripting?
Cannelloni or spaghetti?Cannelloni or spaghetti?
Ease of scripting threatens disciplined scripting
Everybody thinks they know how to handle spaghetti
Everybody thinks they know how to handle spaghetti
Teach Ruby on Rails for web scripting
Cannelloni beats spaghettiCannelloni beats spaghetti
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/
Ruby on Rails: Structure for web scripting
Filled cannelloni beats saucy spaghetti
Filled cannelloni beats saucy spaghetti
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%’
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/