Rails Intro & Tutorial
Upcoming SlideShare
Loading in...5
×
 

Rails Intro & Tutorial

on

  • 745 views

A rails introduction and tutorial course for FJU

A rails introduction and tutorial course for FJU

Statistics

Views

Total Views
745
Views on SlideShare
745
Embed Views
0

Actions

Likes
3
Downloads
18
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rails Intro & Tutorial Rails Intro & Tutorial Presentation Transcript

  • Ruby On Rails Intro & Tutorial Mason ChangSaturday, April 13, 13
  • Who I am? • Mason Chang (張銘軒) • Works at OptimisDev • twitter: @changmason • github: https://github.com/changmasonSaturday, April 13, 13
  • Part ISaturday, April 13, 13
  • What is Rails?Saturday, April 13, 13
  • Rails is... http://rubyonrails.org/Saturday, April 13, 13
  • Rails is... • Created by DHH of 37signals in 2004 • Extracted from product Basecamp • Of course, written in RubySaturday, April 13, 13
  • Rails PhilosophySaturday, April 13, 13
  • Dont Repeat Yourself • Code generators • Rake tasks • ExtentionsSaturday, April 13, 13
  • Code generators • Generate new project • Generate models • Generate controllers & viewsSaturday, April 13, 13
  • Rake tasks • Run tests • Manipulate database • Show useful infoSaturday, April 13, 13
  • Extensions • Sensible core extensions • Helper methods • A lot of 3rd party gemsSaturday, April 13, 13
  • Convention over Configuration(1) • Convention for environments development, test and production • Convention for organizing code • Convention for organizing assetsSaturday, April 13, 13
  • Convention over Configuration(2) • Convention for models model names - table names model attributes - record fields • Convention for controllers & views controller actions - view templatesSaturday, April 13, 13
  • MVC ArchitectureSaturday, April 13, 13
  • http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/Saturday, April 13, 13
  • RESTful RoutesSaturday, April 13, 13
  • Combine HTTP verb and path to dispatch requests to corresponding handlers (actions) http://guides.rubyonrails.org/routing.htmlSaturday, April 13, 13
  • A very simple blog in 5 minuteSaturday, April 13, 13
  • Part IISaturday, April 13, 13
  • ActiveRecord & ModelSaturday, April 13, 13
  • What is ActiveRecord? • ORM, Object-Relational Mapping • Dealing with SQL • A standalone Module • Tons of functionalitiesSaturday, April 13, 13
  • $ gem install activerecord $ irb > require rubygems > require active_record > ActiveRecord::VERSION::STRINGSaturday, April 13, 13
  • ActiveRecord can connect to existing DB > ActiveRecord::Base.establish_connection( :adapter => mysql2, :database => fju_test_db, :username => root, :password => ****** ) > ActiveRecord::Base.connection > ActiveRecord::Base.connected?Saturday, April 13, 13
  • ActiveRecord can migrate existing DB > ARConnection = AR::Base.connection > ARConnection.create_table(:users) do |t| t.string :name t.integer :age t.timestamps end > ARConnection.add_index(:users, name)Saturday, April 13, 13
  • ActiveRecord can execute raw SQL > ARConnection = AR::Base.connection > ARConnection.execute(%q{ INSERT INTO users (name, age) VALUES (Mason Chang, 30) }) > resultes = ARConnection.execute(%q{ SELECT * FROM users WHERE age = 30 }) > results.fields > results.firstSaturday, April 13, 13
  • User Model > class User < ActiveRecord::Base end > require logger > ActiveRecord::Base.logger = Logger.new(STDOUT)Saturday, April 13, 13
  • Model can create records > user.newser = User.new( :name => Eddie Kao, :age => 20) > user.save > User.create( :name => Ryudo Teng, :age => 20)Saturday, April 13, 13
  • Model can retrieve records > User.first > User.last > User.all > User.find(1) > User.find([1,2,3]) > User.find(:first, :conditions => {:name => Eddie Kao}) > User.find(:all, :conditions => [age < ?, 30])Saturday, April 13, 13
  • Model can update records > user = User.find_by_name(Mason Chang) > user.age = 10 > user.save > user.update_attribute(:age, 20) > user.update_attributes( :name => Ming-hsuan Chang :age => 30) > User.update(1, :age => 40) > User.update_all( {:age => 50}, [age >= ?, 20])Saturday, April 13, 13
  • Model can delete records > user = User.first > user.delete # callbacks ignored > user.destroy # callbacks triggered > User.delete(1) # callbacks ignored > User.destroy(1) # callbacks triggeredSaturday, April 13, 13
  • Model can do validations > class User validates_presence_of :name validates_numericality_of :age end > user = User.new(:age => thirty") > user.save # fail > user.errors.messages http://guides.rubyonrails.org/active_record_validations_callbacks.htmlSaturday, April 13, 13
  • Model can have callbacks • Callbacks are operations which hooked into the lifecycle of an ActiveRecord object • Objects lifecycle includes: creating, updating and destroying an object after initializing and finding an object http://guides.rubyonrails.org/active_record_validations_callbacks.htmlSaturday, April 13, 13
  • Callbacks for creating an object • before_validation • after_validation • before_save • around_save • before_create • around_create • after_create • after_saveSaturday, April 13, 13
  • Callbacks for updating an object • before_validation • after_validation • before_save • around_save • before_update • around_update • after_update • after_saveSaturday, April 13, 13
  • Callbacks for destroying an object • before_destroy • around_destroy • after_destroySaturday, April 13, 13
  • Model can have associations • One-to-one • One-to-many • Many-to-many • and more... http://guides.rubyonrails.org/association_basics.htmlSaturday, April 13, 13
  • One-to-one associationSaturday, April 13, 13
  • One-to-many associationSaturday, April 13, 13
  • Many-to-many associationSaturday, April 13, 13
  • Part IIISaturday, April 13, 13
  • Task 0: Install RailsSaturday, April 13, 13
  • $ gem install rails $ rails --versionSaturday, April 13, 13
  • The rails command • create new project > rails new [project_name] • start server > rails server • start console > rails console • execute generator > rails generate [generator_type] • more, and get help > rails help [command]Saturday, April 13, 13
  • Task 1: New ProjectSaturday, April 13, 13
  • $ rails new mylibrary --skip-test-unit $ cd mylibrary/ $ rails server $ rm public/index.htmlSaturday, April 13, 13
  • Project Directory(1)Saturday, April 13, 13
  • Project Directory(2)Saturday, April 13, 13
  • Task 2: Install Some Gems & Import SpecsSaturday, April 13, 13
  • Gemfile: add haml-rails gem $ bundle install Gemfile: add rspec-rails Gemfile: add capybara $ bundle install $ rails generate rspec:install spec/spec_helper.rb: require capybara-railsSaturday, April 13, 13
  • Task 3: Create Book ModelSaturday, April 13, 13
  • $ rails generate model Book author:string title:string description:text publish_date:date $ rake db:migrate $ rails console > Book.column_namesSaturday, April 13, 13
  • Task 4: Pass the "Create" Book SpecSaturday, April 13, 13
  • Task 5: Pass the "Retrieve" Book SpecSaturday, April 13, 13
  • Task 6: Pass the "Update" Book SpecSaturday, April 13, 13
  • Task 7: Pass the "Destroy" Book SpecSaturday, April 13, 13
  • Task 8: Refactor RoutesSaturday, April 13, 13
  • Task 9: Refactor ViewsSaturday, April 13, 13
  • Task 10: Scaffold the CategorySaturday, April 13, 13
  • $ rails generate model Category name:string $ rake db:migrate $ rails console > Category.column_namesSaturday, April 13, 13
  • Task 11: Apply Models AssociationsSaturday, April 13, 13
  • class Category < ActiveRecord::Base has_many :books end class Book < ActiveRecord::Base belongs_to :category endSaturday, April 13, 13
  • $ rails generate migration add_category_id_to_books category_id:integer:index $ rake db:migrateSaturday, April 13, 13
  • Task 12: Validate InputsSaturday, April 13, 13
  • class Book < ActiveRecord::Base belongs_to :category validates_presence_of :category_id, :author, :title endSaturday, April 13, 13
  • Task 13: Decorate with Twitter BootstrapSaturday, April 13, 13
  • Task 14: Deploy to HerokuSaturday, April 13, 13
  • AssignmentsSaturday, April 13, 13
  • Assignment 1: Add a search bar above the book list table on the index page, so the user can search for books by author name or by book title.Saturday, April 13, 13
  • Assignment 2: Add pagination links below the book list table on the index page, so the user can browse through 20 books per page.Saturday, April 13, 13