1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.

Slides:



Advertisements
Similar presentations
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Advertisements

Trestle Generator Industrial-strength scaffolding for Ruby on Rails web application development.
Creating rails app. p:\InstantRails\rails_apps>rails -d mysql cars create create app/controllers create app/helpers create app/models create app/views/layouts.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Methods for Rails. File Structures This is taken directly from app Holds all the code that's specific.
A complete ror application. Some easy tutorials This uses oracle but it is easy to replace that with mysql:
Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Intro to Rails INFO 2310: Topics in Web Design and Programming.
DB Audit Expert v1.1 for Oracle Copyright © SoftTree Technologies, Inc. This presentation is for DB Audit Expert for Oracle version 1.1 which.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
MySql In Action Step by step method to create your own database.
Rails and Grails. To get started Make sure you have java installed You can get the sdk and jre at:
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Access Lesson 2 Creating a Database
CPSC 203 Introduction to Computers T59 & T64 By Jie (Jeff) Gao.
ASP.NET Programming with C# and SQL Server First Edition
Software Engineering 2003 Jyrki Nummenmaa 1 CASE Tools CASE = Computer-Aided Software Engineering A set of tools to (optimally) assist in each.
Taking ActiveRecord to the Next Level Blythe Dunham
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
1 Dr Alexiei Dingli Web Science Stream Helpers, Forms and Layouts.
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
CAKEPHP Blog tutorial. what you’ll need examples/blog/blog.html 2  A running web server  A database server.
Putting it all together Dynamic Data Base Access Norman White Stern School of Business.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
Associations INFO 2310: Topics in Web Design and Programming.
1 Dr Alexiei Dingli Web Science Stream A ROR Blog.
Photo Gallery INFO 2310: Topics in Web Design and Programming.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
Ruby on Rails: Databases. Rails Database Familiar Table Concept Naming convention – lower case, plural (i.e. tweets) How to Access (find), Update, Delete.
1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.
Web Application Development Technology เทคโนโลยีสำหรับการพัฒนาโปรแกรม ประยุกต์เว็บ Suntorn Witosurapot Phone: or
1 Dr Alexiei Dingli Web Science Stream Web We’ll implement a voting mechanism Using AJAX Web 2.0.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
1 Migration. 2 What’s Migration? Migration –Isolates database differences Allows you to write schema updates without worries about differences –Helps.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
CPSC 203 Introduction to Computers T97 By Jie (Jeff) Gao.
1 Dr Alexiei Dingli Web Science Stream Installing ROR.
Adding Data to a Database Table Carol Wolf Computer Science.
Rails and routing INFO 2310: Topics in Web Design and Programming.
Ruby on Rails & Databases. Active Record Active Record in Rails CRUD & Other Stuff Mapping Cardinalities Migrations Demo.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.
Migrations Carol Wolf CS 396X. ISBNTitleAuthorImage EmmaAustenemma.jpg Oliver TwistDickenstwist.jpg HamletShakespearehamlet.jpg.
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
Advanced Migration By Aye Mon Tun.  To change database schema in consistent and easy way  In ruby code Migration? 11/25/2013 2Web Application Engineering.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
CMPE/SE 131 Software Engineering September 1 Class Meeting Department of Computer Engineering San José State University Fall 2016 Instructor: Ron Mak
SQL and SQL*Plus Interaction
ISC440: Web Programming 2 Server-side Scripting PHP 3
Agile Web Development with Ruby and Rails
Model for Student Table
Chapter 15 Introduction to Rails.
Computer Science Projects Database Theory / Prototypes
CMPE/SE 131 Software Engineering February 2 Class Meeting
Presentation transcript:

1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers

2 Case Study: digg

3 rails shovell Creating our shovell application

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

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/ _create_stories.rb The output should be...

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 _create_stories.rb –A migration file Let’s look at the output

7 Lightweight format to represent data Has the.yml extension Have a look at the test/fixtures/stories.yml YAML

8 # Read about fixtures at one: name: MyString link: MyString two: name: MyString link: MyString stories.yml

9 # Read about fixtures at one: name: My web site link: two: name: Other web site link: stories.yml

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!

11 class CreateStories < ActiveRecord::Migration def self.up create_table :stories do |t| t.string :name t.string :link t.timestamps end def self.down drop_table :stories end _create_stories.rb

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

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

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

15 == CreateStories: migrating ================== -- create_table(:stories) -> s == CreateStories: migrated (0.0050s) ========= If it is successful we will find a stories table in our shovell database The output

16 rake db:migrate version=n Eg: undo all the tables in the database by invoking: –rake db:migrate version=0 Rolling back

17 Open a rails console –ruby script/console Playing with the data

18 s = Story.new s.name = “My new website” s.link = “ s.save The end result should be => true Creating our first record

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 => ‘ More on records...

20 Story.find(2) Story.find(:all) Story.find(:all).last Story.find(:first, :order => ‘id DESC’) Retrieving records

21 Story.find_by_name(‘Abc’) How would we find the link ‘ Try it... Dynamic finders...

22 s = Story.find_by_name(‘Abc’) s.name = ‘Abcd’ s.save Let’s update

23 s = Story.find_by_name(‘Abcd’) s.update_attribute :name. ‘Abcde’ Let’s update and save in just one step

24 s.destroy Try to find the record... what’s the message? Bye Bye records

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?

26 ruby script/generate controller Stories index Generating our first controller!

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

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

29 Start a server –ruby script/server Goto – Let’s see what we have so far...

30 Two ways... With or Without scaffolding! Creating views

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?

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

33 ruby script/generate scaffold Story name:String link:String Let’s scaffold!

34 Start a server –ruby script/server Goto – Let’s see what we have so far...

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

36 Start a server –ruby script/server Goto – Let’s see what we have so far...

37 ruby script/generate model Story name:string link:string ruby script/generate controller Stories index Ohh No!! We lost everything!!

38 Start a server –ruby script/server Goto – Let’s see what we have so far...

39 app/views/stories –Only index.html.erb so far –Generated as a static page –Let’s add some dynamic information Insert – Views

40 Start a server –ruby script/server Goto – Let’s see what we have so far...

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 = Time.now –In the app/views/stories/index.html.erb replace the previous code with – –Try it out! Problems!

42 In the controller /app/controllers/stories_controller.rb –In the def index, remove what we just wrote and = Story.find(:first, :order => ‘RANDOM()’) –In the app/views/stories/index.html.erb replace the previous code with –A random link: Let’s do something more useful

43 Start a server –ruby script/server Goto – Let’s see what we have so far...

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 = " >> s.save Didn’t work?

45 Questions?