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

Like this? Share it with your network

Share

Rails Intro & Tutorial

  • 815 views
Uploaded on

A rails introduction and tutorial course for FJU

A rails introduction and tutorial course for FJU

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
815
On Slideshare
815
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
18
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ruby On Rails Intro & Tutorial Mason ChangSaturday, April 13, 13
  • 2. Who I am? • Mason Chang (張銘軒) • Works at OptimisDev • twitter: @changmason • github: https://github.com/changmasonSaturday, April 13, 13
  • 3. Part ISaturday, April 13, 13
  • 4. What is Rails?Saturday, April 13, 13
  • 5. Rails is... http://rubyonrails.org/Saturday, April 13, 13
  • 6. Rails is... • Created by DHH of 37signals in 2004 • Extracted from product Basecamp • Of course, written in RubySaturday, April 13, 13
  • 7. Rails PhilosophySaturday, April 13, 13
  • 8. Dont Repeat Yourself • Code generators • Rake tasks • ExtentionsSaturday, April 13, 13
  • 9. Code generators • Generate new project • Generate models • Generate controllers & viewsSaturday, April 13, 13
  • 10. Rake tasks • Run tests • Manipulate database • Show useful infoSaturday, April 13, 13
  • 11. Extensions • Sensible core extensions • Helper methods • A lot of 3rd party gemsSaturday, April 13, 13
  • 12. Convention over Configuration(1) • Convention for environments development, test and production • Convention for organizing code • Convention for organizing assetsSaturday, April 13, 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
  • 14. MVC ArchitectureSaturday, April 13, 13
  • 15. http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/Saturday, April 13, 13
  • 16. RESTful RoutesSaturday, April 13, 13
  • 17. Combine HTTP verb and path to dispatch requests to corresponding handlers (actions) http://guides.rubyonrails.org/routing.htmlSaturday, April 13, 13
  • 18. A very simple blog in 5 minuteSaturday, April 13, 13
  • 19. Part IISaturday, April 13, 13
  • 20. ActiveRecord & ModelSaturday, April 13, 13
  • 21. What is ActiveRecord? • ORM, Object-Relational Mapping • Dealing with SQL • A standalone Module • Tons of functionalitiesSaturday, April 13, 13
  • 22. $ gem install activerecord $ irb > require rubygems > require active_record > ActiveRecord::VERSION::STRINGSaturday, April 13, 13
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. User Model > class User < ActiveRecord::Base end > require logger > ActiveRecord::Base.logger = Logger.new(STDOUT)Saturday, April 13, 13
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. Callbacks for creating an object • before_validation • after_validation • before_save • around_save • before_create • around_create • after_create • after_saveSaturday, April 13, 13
  • 34. Callbacks for updating an object • before_validation • after_validation • before_save • around_save • before_update • around_update • after_update • after_saveSaturday, April 13, 13
  • 35. Callbacks for destroying an object • before_destroy • around_destroy • after_destroySaturday, April 13, 13
  • 36. 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
  • 37. One-to-one associationSaturday, April 13, 13
  • 38. One-to-many associationSaturday, April 13, 13
  • 39. Many-to-many associationSaturday, April 13, 13
  • 40. Part IIISaturday, April 13, 13
  • 41. Task 0: Install RailsSaturday, April 13, 13
  • 42. $ gem install rails $ rails --versionSaturday, April 13, 13
  • 43. 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
  • 44. Task 1: New ProjectSaturday, April 13, 13
  • 45. $ rails new mylibrary --skip-test-unit $ cd mylibrary/ $ rails server $ rm public/index.htmlSaturday, April 13, 13
  • 46. Project Directory(1)Saturday, April 13, 13
  • 47. Project Directory(2)Saturday, April 13, 13
  • 48. Task 2: Install Some Gems & Import SpecsSaturday, April 13, 13
  • 49. 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
  • 50. Task 3: Create Book ModelSaturday, April 13, 13
  • 51. $ rails generate model Book author:string title:string description:text publish_date:date $ rake db:migrate $ rails console > Book.column_namesSaturday, April 13, 13
  • 52. Task 4: Pass the "Create" Book SpecSaturday, April 13, 13
  • 53. Task 5: Pass the "Retrieve" Book SpecSaturday, April 13, 13
  • 54. Task 6: Pass the "Update" Book SpecSaturday, April 13, 13
  • 55. Task 7: Pass the "Destroy" Book SpecSaturday, April 13, 13
  • 56. Task 8: Refactor RoutesSaturday, April 13, 13
  • 57. Task 9: Refactor ViewsSaturday, April 13, 13
  • 58. Task 10: Scaffold the CategorySaturday, April 13, 13
  • 59. $ rails generate model Category name:string $ rake db:migrate $ rails console > Category.column_namesSaturday, April 13, 13
  • 60. Task 11: Apply Models AssociationsSaturday, April 13, 13
  • 61. class Category < ActiveRecord::Base has_many :books end class Book < ActiveRecord::Base belongs_to :category endSaturday, April 13, 13
  • 62. $ rails generate migration add_category_id_to_books category_id:integer:index $ rake db:migrateSaturday, April 13, 13
  • 63. Task 12: Validate InputsSaturday, April 13, 13
  • 64. class Book < ActiveRecord::Base belongs_to :category validates_presence_of :category_id, :author, :title endSaturday, April 13, 13
  • 65. Task 13: Decorate with Twitter BootstrapSaturday, April 13, 13
  • 66. Task 14: Deploy to HerokuSaturday, April 13, 13
  • 67. AssignmentsSaturday, April 13, 13
  • 68. 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
  • 69. 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