1 dr alexiei dingli web science stream models, views and controllers
TRANSCRIPT
![Page 1: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/1.jpg)
1
Dr Alexiei Dingli
Web Science StreamModels, Views and
Controllers
![Page 2: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/2.jpg)
2
Case Study: digg
![Page 3: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/3.jpg)
3
rails shovell
Creating our shovell application
![Page 4: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/4.jpg)
4
• To generate a new data model for our application we’ll use the comand below
• Our Story model will get two attributes– Name– Link
• String is a type which holds up to 255 alphanumeric characters
• cd shovell• ruby script/generate model Story name:string link:string
Generating our model
![Page 5: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/5.jpg)
5
• exists app/models/• exists test/unit/• exists test/fixtures/• create app/models/story.rb• create test/unit/story_test.rb• create test/fixtures/stories.yml• create db/migrate• create db/migrate/20091019052909_create_stories.rb
The output should be ...
![Page 6: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/6.jpg)
6
• story.rb– In the app/model– Creates a blank ActiveRecord
• story_test.rb– Automatically generated unit testing
• stories.yml– Helps our unit testing and is called a Fixture– Fixtures are files containing simple data for unit testing purposes
• 20091019052909_create_stories.rb– A migration file
Let’s look at the output
![Page 7: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/7.jpg)
7
• Lightweight format to represent data
• Has the .yml extension
• Have a look at the test/fixtures/stories.yml
YAML
![Page 8: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/8.jpg)
8
• # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
• one:• name: MyString• link: MyString
• two:• name: MyString• link: MyString
stories.yml
![Page 9: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/9.jpg)
9
• # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
• one:• name: My web site• link: http://abc.net
• two:• name: Other web site• link: http://www.theotherwebsite.com
stories.yml
![Page 10: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/10.jpg)
10
• Migration files – Used to make modifications to the database
schema– All through Ruby code– No SQL needed– Files are numbered so they can be executed
sequentially– They are executed in order– Located in the db/migrate dir
I’m going to Migrate!
![Page 11: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/11.jpg)
11
• class CreateStories < ActiveRecord::Migration• def self.up• create_table :stories do |t|• t.string :name• t.string :link
• t.timestamps• end• end
• def self.down• drop_table :stories• end• end
20091019052909_create_stories.rb
![Page 12: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/12.jpg)
12
• Change– create_table :stories do |t|
• To– create_table :stories, :force =>true do |t|
• Useful if we already have some table structures defined in the database
Let’s do a small modification
![Page 13: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/13.jpg)
13
• rake is based upon the C make tool• Very versatile and allows us to do a
number of things ...• Try
– rake –T
• In our example we’ll make the migration by invoking– rake db:migrate
Let’s make our migrate
![Page 14: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/14.jpg)
14
1. Checks the database for the most recent migration
2. Steps through the migrations that have not been applied
3. For each migration execute the up method
rake db:migrate
![Page 15: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/15.jpg)
15
== CreateStories: migrating ==================
-- create_table(:stories)
-> 0.0040s
== CreateStories: migrated (0.0050s) =========
• If it is successful we will find a stories table in our shovell database
The output
![Page 16: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/16.jpg)
16
• rake db:migrate version=n
• Eg: undo all the tables in the database by invoking:
– rake db:migrate version=0
Rolling back
![Page 17: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/17.jpg)
17
• Open a rails console
– ruby script/console
Playing with the data
![Page 18: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/18.jpg)
18
• s = Story.new• s.name = “My new website”• s.link = “http://abc.net”• s.save
• The end result should be• => true
Creating our first record
![Page 19: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/19.jpg)
19
• To see the record id– s.id
• To check if its a new record– s.new_record?
• To check the number of Stories in the DB– Story.count
• Another way of creating records– Story.create(
:name => ‘Abc’,
:link => ‘http://www.mysite2.com’)
More on records ...
![Page 20: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/20.jpg)
20
• Story.find(2)
• Story.find(:all)
• Story.find(:all).last
• Story.find(:first, :order => ‘id DESC’)
Retrieving records
![Page 21: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/21.jpg)
21
• Story.find_by_name(‘Abc’)
• How would we find the link ‘http://abc.net’?
• Try it ...
Dynamic finders ...
![Page 22: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/22.jpg)
22
• s = Story.find_by_name(‘Abc’)• s.name = ‘Abcd’• s.save
Let’s update
![Page 23: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/23.jpg)
23
• s = Story.find_by_name(‘Abcd’)• s.update_attribute :name. ‘Abcde’
Let’s update and save in just one step
![Page 24: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/24.jpg)
24
• s.destroy
• Try to find the record ... what’s the message?
Bye Bye records
![Page 25: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/25.jpg)
25
• Have a look at – log/development.log
– CREATE TABLE "stories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "link" varchar(255), "created_at" datetime, "updated_at" datetime
What about SQL?
![Page 26: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/26.jpg)
26
• ruby script/generate controller Stories index
Generating our first controller!
![Page 27: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/27.jpg)
27
• exists app/controllers/• exists app/helpers/• create app/views/stories• exists test/functional/• create test/unit/helpers/• create app/controllers/stories_controller.rb• create test/functional/stories_controller_test.rb• create app/helpers/stories_helper.rb• create test/unit/helpers/stories_helper_test.rb• create app/views/stories/index.html.erb
The output
![Page 28: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/28.jpg)
28
• First it generates a number of folders (unless they have been created already)
• StoriesController– Has defined the index method
• stories_controller_test.rb– Will hold the test functions
• stories_helper.rb– Class to help the controller
• Index.html.erb– One of the views which will be our initial template
Explaining the output
![Page 29: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/29.jpg)
29
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 30: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/30.jpg)
30
Two ways ... With or Without scaffolding!
Creating views
![Page 31: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/31.jpg)
31
• A powerful feature of rails• Quickly creates a web interface for
interacting with your model• Provides an easy way to add, manipulate
and delete records• Scaffold generates a model, controller,
actions and other templates
What is scaffolding?
![Page 32: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/32.jpg)
32
• Designed for quick interaction only• Not intended as a fully automateed web
site generator• It can’t cope with associations
(relationships) between objects
Limitations of Scaffold
![Page 33: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/33.jpg)
33
• ruby script/generate scaffold Story name:String link:String
Let’s scaffold!
![Page 34: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/34.jpg)
34
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 35: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/35.jpg)
35
• Scaffold is essentially a script that we invoke using script/generate
• The nice thing about script/generate is that there exists a script/destroy using exactly the same arguments
• So let’s destroy the scaffold– ruby script/destroy scaffold Story name:String
link:String
Script/generate
![Page 36: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/36.jpg)
36
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 37: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/37.jpg)
37
• ruby script/generate model Story name:string link:string
• ruby script/generate controller Stories index
Ohh No!! We lost everything!!
![Page 38: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/38.jpg)
38
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 39: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/39.jpg)
39
• app/views/stories– Only index.html.erb so far– Generated as a static page– Let’s add some dynamic information
• Insert– <%= Time.now %>
Views
![Page 40: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/40.jpg)
40
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 41: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/41.jpg)
41
• We shouldn’t be including ruby code directly in the view
• Ideally we keep them separated so ...– In the /app/controllers/stories_controller.rb
• In the def index add– @current_time = Time.now
– In the app/views/stories/index.html.erb replace the previous code with
– <%= @current_time %>
– Try it out!
Problems!
![Page 42: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/42.jpg)
42
• In the controller /app/controllers/stories_controller.rb– In the def index, remove what we just wrote
and write• @story = Story.find(:first, :order => ‘RANDOM()’)
– In the app/views/stories/index.html.erb replace the previous code with
– A random link: <%= link_to @story.name, @story.link %>
Let’s do something more useful
![Page 43: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/43.jpg)
43
• Start a server– ruby script/server
• Goto– http://127.0.0.1:3000/stories
Let’s see what we have so far ...
![Page 44: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/44.jpg)
44
• Why not add some data and try again?
• ruby script/console• Loading development environment (Rails 2.3.2)• >> s = Story.new• >> s.name = "ABC"• >> s.link = "http://aaa.com"• >> s.save
Didn’t work?
![Page 45: 1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers](https://reader036.vdocuments.site/reader036/viewer/2022062518/56649e205503460f94b0b442/html5/thumbnails/45.jpg)
45
Questions?