25
Rails provides the structure for disciplined web scripting, or Bruce Scharlau Computing Science Department University of Aberdeen Cannelloni beats spaghetti

Jicc teaching rails

Embed Size (px)

Citation preview

Page 1: Jicc teaching rails

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

There are different languages to choose for web programming

PythonPythonPHPPHP RubyRuby

VBVB JavaJavaC#C#

interpretedinterpreted

compiledcompiled

Page 3: Jicc teaching rails

Picking a language to teach for web scripting can be difficult

versus

Familiar and availableFamiliar and available

unknownunknown

Page 4: Jicc teaching rails

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

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

Learn and teach Ruby on Rails for web scripting

Cannelloni beats spaghettiCannelloni beats spaghetti

Page 7: Jicc teaching rails

Teach how Rails implements the model-view-controller pattern

ControllerController

ModelModel

ViewView

Page 8: Jicc teaching rails

Controllers determine the action

ControllerControllerBrowserBrowser

Page 9: Jicc teaching rails

Models handle the data

ControllerController

DatabaseDatabase

ModelModel

BrowserBrowser

Page 10: Jicc teaching rails

Views serve the presentation

ControllerController

DatabaseDatabase

ModelModel

ViewView

BrowserBrowser

Page 11: Jicc teaching rails

Teach Ruby along with Rails

RubyRuby RailsRails

Page 12: Jicc teaching rails

Teach how to create instances and classes

class ClientController < ApplicationController… def new @customer = Customer.new end …end

Page 13: Jicc teaching rails

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

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

Use a flexible and familiar example

Page 16: Jicc teaching rails

Start with simple components

ruby script/generate scaffold -s Customer Client

Page 17: Jicc teaching rails

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

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

Is there a good structure for teaching web scripting?

Cannelloni or spaghetti?Cannelloni or spaghetti?

Page 20: Jicc teaching rails

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

Teach Ruby on Rails for web scripting

Cannelloni beats spaghettiCannelloni beats spaghetti

Page 22: Jicc teaching rails

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

Ruby on Rails: Structure for web scripting

Filled cannelloni beats saucy spaghetti

Filled cannelloni beats saucy spaghetti

Page 24: Jicc teaching rails

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

Resources:

Bruce Scharlau

[email protected]

http://www.csd.abdn.ac.uk/~bscharla/

Department of Computer Science

University of Aberdeen

Bruce Scharlau

[email protected]

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/