Your SlideShare is downloading. ×
0
Ruby on Rails: An Introduction <ul><li>JA-SIG Summer Conference 2007 </li></ul><ul><li>Michael Irion </li></ul><ul><li>The...
What is Ruby on Rails (RoR)? <ul><li>Ruby on Rails is an open-source, full-stack framework for developing database-backed ...
Overview of Ruby on Rails  <ul><li>Ruby is the programming language used to manipulate the framework </li></ul><ul><li>Rai...
Ruby Features <ul><li>Ruby is an interpreted language  (No compile step necessary) </li></ul><ul><li>Ruby is an Object Ori...
Duck Typing in Ruby <ul><li>Objects are dynamic, and their types are determined at runtime </li></ul><ul><li>The type of a...
Rails Philosophy <ul><li>Convention over configuration (no XML) </li></ul><ul><li>Don’t Repeat Yourself (DRY) </li></ul><u...
Rails Architecture <ul><li>Rails applications are implemented using the Model-View-Controller (MVC)  </li></ul><ul><li>Mod...
Starting the Bookmarks Application <ul><li>Generate application </li></ul><ul><li>> rails bookmarks </li></ul>
Directory Layout <ul><li>Rails applications have a common directory structure  </li></ul><ul><li>/app -  the MVC core </li...
Directory Layout <ul><ul><li>/components - will be deprecated </li></ul></ul><ul><ul><li>/config - database, route and env...
Rails Environment Modes <ul><li>Rails runs in different modes, depending on the parameters given to the server on startup....
Starting Rails <ul><li>> cd  /directorypath/bookmarks </li></ul><ul><li>Start the server </li></ul><ul><li>  > ruby  scrip...
Welcome Aboard - Now What? <ul><li>Hello world seems appropriate </li></ul><ul><li>>ruby script/generate controller hello ...
Mapping URLs to Controllers and Actions <ul><li>http://127.0.0.1:3000/hello/sayit </li></ul><ul><li>http://127.0.0.1:3000 ...
Editing the Controller <ul><li>def sayit </li></ul><ul><li>render :text => &quot;<h2>Hello World!</h2>&quot; </li></ul><ul...
Now for an actual Bookmark <ul><li>Edit config/database.yml </li></ul><ul><li>development: </li></ul><ul><li>adapter: mysq...
Create the Database <ul><li>This step depends on the database and dba tool of your choice.  Create a new schema/dbname for...
Bookmark Model <ul><li>Our bookmark model will (initially) need two properties </li></ul><ul><li>URL </li></ul><ul><li>Tit...
Scaffolding for Bookmarks <ul><li>Rails can generate all the basic CRUD operations for simple models via scaffolding. </li...
Migrations <ul><li>Rails uses migrations to version the database. </li></ul><ul><li>Rails tries to minimize SQL at every o...
Bookmarks Migration <ul><li>located in db/migrate/001_create_bookmarks.rb </li></ul><ul><li>class CreateBookmarks < Active...
Running the Migration <ul><li>Rake is the general purpose build tool for rails, much like make, or ant.  It has many funct...
Bookmarks Table ID <ul><li>Bookmarks table has the following fields - id, url, and title </li></ul><ul><li>Where did the i...
Bookmarks Controller <ul><li>The /app/controllers/bookmarks.rb  default action: </li></ul><ul><li>def index </li></ul><ul>...
Mapping URLs to Controllers and Actions <ul><li>http://127.0.0.1:3000/bookmarks/ </li></ul><ul><li>http://127.0.0.1:3000  ...
Bookmarks Model - Don’t Repeat Yourself <ul><li>No getters/setters.  Rails uses information from the database. </li></ul><...
Bookmarks View <ul><li>Located in views/bookmarks/index.rhtml </li></ul><ul><li><% for bookmark in @bookmarks %> </li></ul...
Convention over Configuration <ul><li>Bookmark model automatically looks for a table in the database with a plural form of...
Ajax Helpers <ul><li>Rails allows you to program many AJAX calls in ruby, instead of writing javascript directly </li></ul...
AJAX Autocomplete <ul><li>Add to Bookmarks controller </li></ul><ul><li>auto_complete_for :bookmarks, :url </li></ul><ul><...
Validations <ul><li>Rails has a number of validation helpers that can be added to the model.  </li></ul><ul><li>class Book...
Validations <ul><li>validates_presence_of  </li></ul><ul><li>validates_length_of  </li></ul><ul><li>validates_acceptance_o...
Associations - Adding Categories to Bookmarks <ul><li>The bookmarks are working great, but it would be nice if we could gr...
Types of Associations <ul><li>has_one </li></ul><ul><li>belongs_to </li></ul><ul><li>has_many </li></ul><ul><li>has_and_be...
Changes to the Database <ul><li>A new categories table needs to be created </li></ul><ul><li>A category_id field needs to ...
Alter Bookmarks Migration <ul><li>def self.up </li></ul><ul><li>add_column :bookmarks, :category_id, :integer </li></ul><u...
Types of Associations <ul><li>has_one </li></ul><ul><li>belongs_to </li></ul><ul><li>has_many </li></ul><ul><li>has_and_be...
Database Relationships <ul><li>Parent (or Master) models that have collections of other models use the has_many relationsh...
Associations Model Code  <ul><li>class Bookmark < ActiveRecord::Base </li></ul><ul><li>validates_presence_of :url, :title ...
Associations Controller code <ul><li>def edit </li></ul><ul><li>@bookmark = Bookmark.find(params[:id]) </li></ul><ul><li>@...
Associations View Code <ul><li><p> </li></ul><ul><li><b>Category</b><br /> </li></ul><ul><li><%= collection_select('bookma...
Tools <ul><li>Textmate (Mac OS X) </li></ul><ul><li>RadRails (Eclipse plugin)  www.radrails.org </li></ul><ul><li>Other co...
Resources <ul><li>Programming Ruby: The Pragmatic Programmers' Guide - Second Edition </li></ul><ul><li>Agile Web Developm...
Upcoming SlideShare
Loading in...5
×

Jasig Rubyon Rails

1,578

Published on

originally here:
http://www.ja-sig.org/wiki/download/attachments/6063795/JASIG-RubyonRails.ppt

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • What a great slide! We're looking at message broker solutions right now ... you a top candidate!
    http://www.mediafire.com/download/kx9l74xfsn96454/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
1,578
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide
  • Transcript of "Jasig Rubyon Rails"

    1. 1. Ruby on Rails: An Introduction <ul><li>JA-SIG Summer Conference 2007 </li></ul><ul><li>Michael Irion </li></ul><ul><li>The University of Tulsa </li></ul>
    2. 2. What is Ruby on Rails (RoR)? <ul><li>Ruby on Rails is an open-source, full-stack framework for developing database-backed web applications according to the Model-View-Control pattern </li></ul>
    3. 3. Overview of Ruby on Rails <ul><li>Ruby is the programming language used to manipulate the framework </li></ul><ul><li>Rails is the framework that provides the necessary infrastructure </li></ul><ul><li>Rails is written in Ruby </li></ul>
    4. 4. Ruby Features <ul><li>Ruby is an interpreted language (No compile step necessary) </li></ul><ul><li>Ruby is an Object Oriented language. </li></ul><ul><li>Everything is an object (No primitives) </li></ul><ul><li>Ruby draws from Perl, Smalltalk, and Lisp </li></ul>
    5. 5. Duck Typing in Ruby <ul><li>Objects are dynamic, and their types are determined at runtime </li></ul><ul><li>The type of a Ruby object is much less important than it’s capabilities </li></ul><ul><li>If a Ruby object walks like a duck and talks like a duck, then it can be treated as a duck </li></ul>
    6. 6. Rails Philosophy <ul><li>Convention over configuration (no XML) </li></ul><ul><li>Don’t Repeat Yourself (DRY) </li></ul><ul><li>Rails is opinionated </li></ul>
    7. 7. Rails Architecture <ul><li>Rails applications are implemented using the Model-View-Controller (MVC) </li></ul><ul><li>Model - ActiveRecord </li></ul><ul><li>View - ActionView </li></ul><ul><li>Controller - ActionController </li></ul>
    8. 8. Starting the Bookmarks Application <ul><li>Generate application </li></ul><ul><li>> rails bookmarks </li></ul>
    9. 9. Directory Layout <ul><li>Rails applications have a common directory structure </li></ul><ul><li>/app - the MVC core </li></ul><ul><li> /controllers </li></ul><ul><li> /helpers - provides extra functionality for views </li></ul><ul><li> /models </li></ul><ul><li> /views/nameofcontroller - templates for controller actions </li></ul>
    10. 10. Directory Layout <ul><ul><li>/components - will be deprecated </li></ul></ul><ul><ul><li>/config - database, route and environment configuration </li></ul></ul><ul><ul><li>/db - database schema and migrations </li></ul></ul><ul><ul><li>/lib - functions that don’t map to MVC </li></ul></ul><ul><ul><li>/log </li></ul></ul><ul><ul><li>/public - static web resources (html, css, javascript etc.) </li></ul></ul><ul><ul><li>/script - rails utilities </li></ul></ul><ul><ul><li>/test - tests and fixtures </li></ul></ul><ul><ul><li>/tmp </li></ul></ul><ul><ul><li>/vendor - 3rd party plugins </li></ul></ul>
    11. 11. Rails Environment Modes <ul><li>Rails runs in different modes, depending on the parameters given to the server on startup. Each mode defaults to it’s own database schema </li></ul><ul><li>Development (verbose logging and error messages) </li></ul><ul><li>Test </li></ul><ul><li>Production </li></ul>
    12. 12. Starting Rails <ul><li>> cd /directorypath/bookmarks </li></ul><ul><li>Start the server </li></ul><ul><li> > ruby script/server start </li></ul><ul><li>Default environment is development </li></ul><ul><li>Default port is 3000 </li></ul><ul><li>http://127.0.0.1:3000 </li></ul>
    13. 13. Welcome Aboard - Now What? <ul><li>Hello world seems appropriate </li></ul><ul><li>>ruby script/generate controller hello </li></ul><ul><li> exists app/controllers/ </li></ul><ul><li> exists app/helpers/ </li></ul><ul><li> create app/views/hello </li></ul><ul><li> exists test/functional/ </li></ul><ul><li> create app/controllers/hello_controller.rb </li></ul><ul><li> create test/functional/hello_controller_test.rb </li></ul><ul><li> create app/helpers/hello_helper.rb </li></ul>
    14. 14. Mapping URLs to Controllers and Actions <ul><li>http://127.0.0.1:3000/hello/sayit </li></ul><ul><li>http://127.0.0.1:3000 - address and port of the webserver </li></ul><ul><li>hello - name of the controller </li></ul><ul><li>sayit - name of the action (method in controller) </li></ul>
    15. 15. Editing the Controller <ul><li>def sayit </li></ul><ul><li>render :text => &quot;<h2>Hello World!</h2>&quot; </li></ul><ul><li>end </li></ul>
    16. 16. Now for an actual Bookmark <ul><li>Edit config/database.yml </li></ul><ul><li>development: </li></ul><ul><li>adapter: mysql </li></ul><ul><li>database: bookmarks_development </li></ul><ul><li>username: username </li></ul><ul><li>password: password </li></ul><ul><li>host: localhost </li></ul>
    17. 17. Create the Database <ul><li>This step depends on the database and dba tool of your choice. Create a new schema/dbname for bookmarks_development, and assign rights for the user you listed in database.yml. </li></ul>
    18. 18. Bookmark Model <ul><li>Our bookmark model will (initially) need two properties </li></ul><ul><li>URL </li></ul><ul><li>Title </li></ul>
    19. 19. Scaffolding for Bookmarks <ul><li>Rails can generate all the basic CRUD operations for simple models via scaffolding. </li></ul><ul><li>Scaffolding is temporary way to get applications wired quickly. </li></ul><ul><li>> ruby script/generate scaffold_resource bookmark url:string title:string </li></ul>
    20. 20. Migrations <ul><li>Rails uses migrations to version the database. </li></ul><ul><li>Rails tries to minimize SQL at every opportunity </li></ul><ul><li>Migrations are automatically created whenever you generate a new model </li></ul><ul><li>Migration files are located in db/migrations </li></ul><ul><li>The version number is stored in a table called schema_info </li></ul>
    21. 21. Bookmarks Migration <ul><li>located in db/migrate/001_create_bookmarks.rb </li></ul><ul><li>class CreateBookmarks < ActiveRecord::Migration </li></ul><ul><li>def self.up </li></ul><ul><li>create_table :bookmarks do |t| </li></ul><ul><li>t.column :url, :string </li></ul><ul><li>t.column :title, :string </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>def self.down </li></ul><ul><li>drop_table :bookmarks </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    22. 22. Running the Migration <ul><li>Rake is the general purpose build tool for rails, much like make, or ant. It has many functions, one of which is to control migrations. </li></ul><ul><li>>rake db:migrate </li></ul><ul><li>Now the table has been created </li></ul>
    23. 23. Bookmarks Table ID <ul><li>Bookmarks table has the following fields - id, url, and title </li></ul><ul><li>Where did the id field come from? </li></ul><ul><li>Convention of configuration - Rails automatically creates an id field for each new table and uses it as the primary key </li></ul>
    24. 24. Bookmarks Controller <ul><li>The /app/controllers/bookmarks.rb default action: </li></ul><ul><li>def index </li></ul><ul><li>@bookmarks = Bookmark.find(:all) </li></ul><ul><li>respond_to do |format| </li></ul><ul><li>format.html # index.rhtml </li></ul><ul><li>format.xml { render :xml => @bookmarks.to_xml } </li></ul><ul><li>end </li></ul><ul><li>End </li></ul>
    25. 25. Mapping URLs to Controllers and Actions <ul><li>http://127.0.0.1:3000/bookmarks/ </li></ul><ul><li>http://127.0.0.1:3000 - address and port of the webserver </li></ul><ul><li>hello - name of the controller </li></ul><ul><li>/ - name of the action (blank maps to the index action) </li></ul>
    26. 26. Bookmarks Model - Don’t Repeat Yourself <ul><li>No getters/setters. Rails uses information from the database. </li></ul><ul><li>class Bookmark < ActiveRecord::Base </li></ul><ul><li>end </li></ul>
    27. 27. Bookmarks View <ul><li>Located in views/bookmarks/index.rhtml </li></ul><ul><li><% for bookmark in @bookmarks %> </li></ul><ul><li><tr> </li></ul><ul><li><td><%=h bookmark.url %></td> </li></ul><ul><li><td><%=h bookmark.title %></td> </li></ul><ul><li><td><%=h bookmark.description %></td> </li></ul><ul><li><td><%= link_to 'Show', bookmark_path(bookmark) %></td> </li></ul><ul><li><td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td> </li></ul><ul><li><td><%= link_to 'Destroy', bookmark_path(bookmark), :confirm => 'Are you sure?', :method => :delete %></td> </li></ul><ul><li></tr> </li></ul><ul><li><% end %> </li></ul>
    28. 28. Convention over Configuration <ul><li>Bookmark model automatically looks for a table in the database with a plural form of it’s name. (bookmarks) </li></ul><ul><li>The Bookmarks controller automatically renders the template located in views according to the controller name and the action (views/bookmarks/index.rhtml) </li></ul>
    29. 29. Ajax Helpers <ul><li>Rails allows you to program many AJAX calls in ruby, instead of writing javascript directly </li></ul><ul><li>Script.aculo.us and Prototype libraries are included </li></ul><ul><li>A quick example. Autocomplete for text boxes </li></ul>
    30. 30. AJAX Autocomplete <ul><li>Add to Bookmarks controller </li></ul><ul><li>auto_complete_for :bookmarks, :url </li></ul><ul><li>Add to views/bookmarks/edit.rhtml </li></ul><ul><li><%= text_field_with_auto_complete :bookmark, :url %> </li></ul><ul><li>In views/layouts/bookmarks.rhtml, add </li></ul><ul><li><%= javascript_include_tag :defaults %> </li></ul>
    31. 31. Validations <ul><li>Rails has a number of validation helpers that can be added to the model. </li></ul><ul><li>class Bookmark < ActiveRecord::Base </li></ul><ul><li>validates_presence_of :url, :title </li></ul><ul><li>end </li></ul>
    32. 32. Validations <ul><li>validates_presence_of </li></ul><ul><li>validates_length_of </li></ul><ul><li>validates_acceptance_of </li></ul><ul><li>validates_confirmation_of </li></ul><ul><li>validates_uniqueness_of </li></ul><ul><li>validates_format_of </li></ul><ul><li>validates_numericality_of </li></ul><ul><li>validates_inclusion_in </li></ul><ul><li>validates_exclusion_of </li></ul><ul><li>validates_associated :relation </li></ul>
    33. 33. Associations - Adding Categories to Bookmarks <ul><li>The bookmarks are working great, but it would be nice if we could group them by category </li></ul><ul><li>Rails uses associations to build relationships between tables </li></ul><ul><li>Associations are independent of database foreign key constraints </li></ul>
    34. 34. Types of Associations <ul><li>has_one </li></ul><ul><li>belongs_to </li></ul><ul><li>has_many </li></ul><ul><li>has_and_belongs_to_many </li></ul><ul><li>has_many :model1, :through => :model2 </li></ul>
    35. 35. Changes to the Database <ul><li>A new categories table needs to be created </li></ul><ul><li>A category_id field needs to be added to the bookmarks table </li></ul><ul><li>> ruby script/generate scaffold_resource category name:string </li></ul><ul><li>This creates the all the scaffolding and the migration db/migrate/002_create_categories.rb </li></ul><ul><li>Note the category table is pluralized as categories. </li></ul><ul><li>>ruby script/generate migration alter_bookmarks_add_category_id </li></ul><ul><li>This creates db/migrate/003_alter_bookmarks_add_category_id.rb </li></ul>
    36. 36. Alter Bookmarks Migration <ul><li>def self.up </li></ul><ul><li>add_column :bookmarks, :category_id, :integer </li></ul><ul><li>end </li></ul><ul><li>def self.down </li></ul><ul><li>remove_column :bookmarks, :category_id </li></ul><ul><li>end </li></ul><ul><li>>rake db:migrate </li></ul>
    37. 37. Types of Associations <ul><li>has_one </li></ul><ul><li>belongs_to </li></ul><ul><li>has_many </li></ul><ul><li>has_and_belongs_to_many </li></ul><ul><li>has_many :model1, :through => :model2 </li></ul>
    38. 38. Database Relationships <ul><li>Parent (or Master) models that have collections of other models use the has_many relationship </li></ul><ul><li>Child (or Detail) models contain the id field of their parent record and they use the belongs_to relationship </li></ul>
    39. 39. Associations Model Code <ul><li>class Bookmark < ActiveRecord::Base </li></ul><ul><li>validates_presence_of :url, :title </li></ul><ul><li>belongs_to :category </li></ul><ul><li>end </li></ul><ul><li>class Category < ActiveRecord::Base </li></ul><ul><li>has_many :bookmarks </li></ul><ul><li>end </li></ul>
    40. 40. Associations Controller code <ul><li>def edit </li></ul><ul><li>@bookmark = Bookmark.find(params[:id]) </li></ul><ul><li>@categories = Category.find(:all) </li></ul><ul><li>end </li></ul>
    41. 41. Associations View Code <ul><li><p> </li></ul><ul><li><b>Category</b><br /> </li></ul><ul><li><%= collection_select('bookmark', 'category_id', @categories, 'id', 'name') %> </li></ul><ul><li></p> </li></ul>
    42. 42. Tools <ul><li>Textmate (Mac OS X) </li></ul><ul><li>RadRails (Eclipse plugin) www.radrails.org </li></ul><ul><li>Other commercial and opensource IDEs are being made available </li></ul>
    43. 43. Resources <ul><li>Programming Ruby: The Pragmatic Programmers' Guide - Second Edition </li></ul><ul><li>Agile Web Development with Rails—Second Edition </li></ul><ul><li>Rails main site http://www.rubyonrails.com </li></ul><ul><li>My email: michael-irion@utulsa.edu </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×