CS3157: Advanced Programming Lecture # rails Apr 30 Shlomo - - PowerPoint PPT Presentation

cs3157 advanced programming
SMART_READER_LITE
LIVE PREVIEW

CS3157: Advanced Programming Lecture # rails Apr 30 Shlomo - - PowerPoint PPT Presentation

CS3157: Advanced Programming Lecture # rails Apr 30 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Ruby on rails Step by step examples today, please pay attention and keep up Ask if we do something you dont understand 2


slide-1
SLIDE 1

1

CS3157: Advanced Programming

Lecture # rails Apr 30

Shlomo Hershkop shlomo@cs.columbia.edu

slide-2
SLIDE 2

2

Outline

Ruby on rails Step by step examples today, please pay

attention and keep up

Ask if we do something you don’t

understand

slide-3
SLIDE 3

3

Background

We covered ruby on Monday

Went kind of fast Would not have learned it if you didn’t know

perl

Although wont test you, should still read it

  • ver and understand at least how to read ruby

basic code

Lets take it to the next level

slide-4
SLIDE 4

4

Install

You need to install the frameworks Gems

Install interface to rubyforge.org gem list --local gem install rails --include-dependencies

slide-5
SLIDE 5

5

Create a test directory

C: \ test

Change there

cd c: \ test

Create framework

Rails ap3157 Will see bunch of stuff

cd ap3157 ruby script\ server

slide-6
SLIDE 6

6

test

Should be able to see something here on port

3000

http: / / 127.0.0.1: 3000/ This is the webbrick server Written completely in ruby Excellent for testing Real world:

Apache Lighttpd

  • thers
slide-7
SLIDE 7

7

forward

cd into the project directory

c: \ test\ ap3157

ruby script/generate controller Greeting

Will setup initial files we need Ok so lets explain what we are looking at

slide-8
SLIDE 8

8

Explanation

http: / / www.some.com/ shopping_cart/ total/ 45

Controller Action Resource Web Server

slide-9
SLIDE 9

9

Setup

Each web server running ruby has one

dispatcher

List of available controllers to handle

requests

Default action is index

Like in html, default file will be index.htm or

index.html

slide-10
SLIDE 10

10

slide-11
SLIDE 11

11

So now go to http: / / 127.0.0.1: 3000/ greeting What is the error ??

slide-12
SLIDE 12

12

Fix ?

Lets define the index action in the

greeting controller

In the controllergreeting class:

def index render :text => "<h1>Welcome to your first Rails application<h1>" end

slide-13
SLIDE 13

13

Side note

Excellent docs in http: / / api.rubyonrails.com.

slide-14
SLIDE 14

14

Want to separate view from controller So lets create a view

  • ruby script/generate controller Greeting index

Answer n

slide-15
SLIDE 15

15

Change greet_controller

Update the class and then reload: class GreetingController < ApplicationController def index end end

slide-16
SLIDE 16

16

Now add a variable to the controller

@welcome_message = "Welcome to your first Rails application"

Render it in the view rhtml file:

<h1><%= @welcome_message %></h1>

slide-17
SLIDE 17

17

<%= %> tags

< % = % >

Can contain either Script-lets Ruby Expressions

slide-18
SLIDE 18

18

Try

Add to greeting

class GreetingController < ApplicationController def index @age=8 @table={"headings" => ["addend", "addend", "sum"], "body" => [[1, 1, 2], [1, 2, 3], [ 1, 3, 4]] } end end

slide-19
SLIDE 19

19

<h1>A simple table</h1> <table> <tr> <% @table["headings"].each do |head| %> <td> <b><%= head %></b> </td> <% end %> </tr> <% @table["body"].each do |row| %> <tr> <% row.each do |col| %> <td> <%= col %> </td> <% end %> </tr> <% end %> </table>

slide-20
SLIDE 20

20

Active record

Create an idea of something in the

datbase

Has predefined function which can be used Don’t need to worry about underlying tech

slide-21
SLIDE 21

21

Goal

Say we want to build a system which will

let us share recipes or photos online

slide-22
SLIDE 22

22

Databases

One of the powers to Ruby is the ability to

interact with databases

Lets go over some of the basics

slide-23
SLIDE 23

23

Database system

DBMS Database management system:

Software package to manage a database Data independence and efficient access. Reduced application development time. Data integrity and security. Uniform data administration. Concurrent access, recovery from crashes.

slide-24
SLIDE 24

24

Data Model

A data model is a collection of concepts for

describing data.

A schema is a description of a particular

collection of data, using a given data model.

The relational model of data is the most

widely used model today.

Main concept: relation, basically a table with

rows and columns.

Every relation has a schema, which describes

the columns, or fields

slide-25
SLIDE 25

25

University Database

Conceptual schema:

Students(sid: string, name: string, login:

string, age: integer, gpa: real)

Courses(cid: string, cname: string,

credits: integer)

Enrolled(sid: string, cid: string, grade: string)

Physical schema:

Relations stored as unordered files. Index on first column of Students.

External Schema (View):

Course_info(cid: string,enrollment: integer)

slide-26
SLIDE 26

26

Constraints

Some rules on how certain parts of the

data can be stored

Kind of data for each type ‘uniqueness’ of certain data items

Keys Foreign keys

Transaction constraints

slide-27
SLIDE 27

27

index

Specific parts of the data we are

interested in gaining quicker access to

Specialized data structures

Usually B+ trees

  • verhead
slide-28
SLIDE 28

28

Other points

Concurrency Atomic action Log system Roll back Integrity guarantees Design issues

slide-29
SLIDE 29

29

Side note

Ruby allows you to define undefined functions

class Talker def method_missing(method) if method.to_s =~ /say_/ puts $' end end end

slide-30
SLIDE 30

30

Active records are ruby classes which

allows you to interact with the DB

It will grab undefined functions as table

columns if possible

Plus other built in stuff:

  • Photo.find_by_filename("balboa_park.jpg").destroy
slide-31
SLIDE 31

31

Recipe website

Lets use Ruby-Rails to create a website to

share recipes

Display a list of all recipes. Create new recipes and edit existing

recipes.

Assign a recipe to a category (like

"dessert" or "soup").

slide-32
SLIDE 32

32

Getting started

Create a test directory and cd there rails cookbook

This will create the framework for our project Will step through specific parts, but very

powerful and very easy to use

Encouraged to go online and explore full

potential after your finals

slide-33
SLIDE 33

33

next

Lets get started with webbrick cd into cookbook directory Ruby script\ server To check:

http: / / 127.0.0.1: 3000/

slide-34
SLIDE 34

34

Apps subdirectory

Controllers

where Rails looks to find controller classes. A controller

handles a web request from the user.

Views

holds the display templates to fill in with data from our

application, convert to HTML, and return to the user's browser.

Models

holds the classes that model and wrap the data stored in our

application's database. In most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple!

Helpers

holds any helper classes used to assist the model, view, and

controller classes. This helps to keep the the model, view, and controller code small, focused, and uncluttered.

slide-35
SLIDE 35

35

ruby script\ generate controller Mytest Read the output What is happening ??

slide-36
SLIDE 36

36

next

Open the mytest controller and add an

index action

How to trigger other events ?

slide-37
SLIDE 37

37

Start with dbase

Lets manually setup the database now Log in to the mysql server running on your

machine

slide-38
SLIDE 38

38

1.

mysql –u root –p

2.

create database cookbook;

3.

use cookbook;

4.

create table recipes (id INT unsigned NOT NULL auto_increment, title varchar(255), instructions text, PRIMARY KEY(id)) engine= InnoDB;

5.

describe recipes;

slide-39
SLIDE 39

39

Back in Ruby

Edit cookbook/ config/ database.yml

development: adapter: mysql database: cookbook username: <your userid> password: <your password> host: localhost

slide-40
SLIDE 40

40

ruby script\generate model Recipe

This is the active record

ruby script\generate controller Recipe Edit recipe_controller.rb

scaffold :recipe

  • http: / / 127.0.0.1: 3000/ recipe/ new
slide-41
SLIDE 41

41

Is everyone ok so far ?

slide-42
SLIDE 42

42

Back to mysql Use coobook; We can add to a table by issuing an alter

command

alter table recipes add column description varchar(255); alter table recipes add column date DATE;

And now…

reload the new event

slide-43
SLIDE 43

43

Add some recipes 127.0.0.0/ recipe/ list Try to add and list recipes

They can be made up

slide-44
SLIDE 44

44

scaffold : recipe

Creates the the actions

list show edit delete

It also created default view templates for each

  • f these actions.

Which means we can now customize them

slide-45
SLIDE 45

45

Edit : recipe_controller.rb def list end Now reload…

what do you see ??

slide-46
SLIDE 46

46

Lets create the template

\ cookbook\ app\ views\ recipe create a file named list.rhtml

slide-47
SLIDE 47

47

< body> < h1> Online Cookbook - All Recipes< / h1> < table border= "1"> < tr> < td width= "80% "> < p align= "center"> < i> < b> Recipe< / b> < / i> < / td> < td width= "20% "> < p align= "center"> < i> < b> Date< / b> < / i> < / td> < / tr> < % @recipes.each do | recipe| % > < tr> < td> < % = link_to recipe.title, : action = > "show", : id = > recipe.id % > < / td> < td> < % = recipe.date % > < / td> < / tr> < % end % > < / table> < p> < % = link_to "Create new recipe", : action = > "new" % > < / p> < / body> < / html>

slide-48
SLIDE 48

48

Now go back and edit the list function def list

@recipes = Recipe.find_all

end

slide-49
SLIDE 49

49

Refresh

127.0.0.1: 3000/ recipe/ list

slide-50
SLIDE 50

50

So how would we add categories ??

slide-51
SLIDE 51

51

Db side

First lets create a database for it

mysql –u root –p use cookbook; create table categories (id INT unsigned NOT NULL auto_increment, name varchar(50), PRIMARY KEY(ID)) engine=InnoDB;

slide-52
SLIDE 52

52

Ruby side shortcut

Next slide shows how to do it manually Here is a huge shortcut ruby script\ generate scaffold Category http: / / 127.0.0.1: 3000/ category/ new

slide-53
SLIDE 53

53

Ruby side

ruby script\ generate controller Category ruby script\ generate model Category Edit cookbook\ app\ controllers\ category_controller.rb Add scaffolding

scaffold :category

Now browse

http: / / 127.0.0.1: 3000/ category/ new

slide-54
SLIDE 54

54

Ok so we can create categories So how to link the two together ?

slide-55
SLIDE 55

55

Lets extend the recipe table by adding a

column called category_id field to the recipe table as an int(6)

Any guesses how its done ?

slide-56
SLIDE 56

56

mysql –u root –p use cookbook alter table recipe add column category_id

int(6);

slide-57
SLIDE 57

57

next

cookbook\ app\ models\ recipe.rb and Add the following:

belongs_to : category

cookbook\ app\ models\ category.rb Add the following:

has_many : recipes

slide-58
SLIDE 58

58

Now

So if I have a recipe object in @recipe, I can find its category name with:

@recipe.category.name.

and if I have a category object in

@category, I can fetch a collection of all recipes in that category using the code @category.recipes..

slide-59
SLIDE 59

59

cookbook\ app\ controllers\ recipe_controller.rb

def list

@recipes = Recipe.find_all

end def edit

@recipe = Recipe.find(@params[“id”]) @categories = Category.find_all

end

slide-60
SLIDE 60

60

Now need to include template for editing

recipes to support this

cookbook\ app\ views\ recipe\ edit.rhtml

slide-61
SLIDE 61

61 <form action="../update/<%= @recipe.id %>" method="POST""> <input id="recipe_id" name="recipe[id]" size="30" type="hidden" value="<%= @recipe.id %>" /> <p><b>Title</b><br> <input id="recipe_title" name="recipe[title]" size="30" type="text" value="<%= @recipe.title %>" /> </p> <p><b>Description</b><br> <input id="recipe_description" name="recipe[description]" size="30" type="text" value="<%= @recipe.description %>" /> </p> <p><b>Category:</b><br> <select name="recipe[category_id]"> <% @categories.each do |category| %> <option value="<%= category.id %>" <%= ' selected' if category.id == @recipe.category_id %>> <%= category.name %> </option> <% end %> </select></p> <p><b>Instructions</b><br> <textarea cols="40" id="recipe_instructions" name="recipe[instructions]" rows="20" wrap="virtual"> <%= @recipe.instructions %> </textarea> </p> <input type="submit" value="Update" /> </form> <a href="/recipe/show/<%= @recipe.id %>"> Show </a> | <a href="/recipe/list"> Back </a>

slide-62
SLIDE 62

62

Bunch of other things, won’t have time to

cover

Type:

Rake stats

Let change delete

<%= link_to "(delete)", {:action => "delete", :id => recipe.id}, :confirm => "Really delete #{recipe.title}?" %>

slide-63
SLIDE 63

63

More info

http: / / www.onlamp.com/ pub/ a/ onlamp/ 2

005/ 01/ 20/ rails.html

http: / / www.onlamp.com/ pub/ a/ onlamp/ 2

005/ 03/ 03/ rails.html

slide-64
SLIDE 64

64

Beyond 3157