Ajmal Afif's blog

Balancing between consuming knowledge and creating values.

MHartl's Ruby on Rails Tutorial

Mhartl’s Ruby on Rails Tutorial (1st Edition) is basically my first programming exposure and experience that I went through as my self-learning process. I bought both screencasts and the pdfs, but I ended up using the screencast as my primary source of learning, and the online book as my fallback in case I’m stuck at point of time (which I had a lot, almost in every chapter). So if you’re an absolute beginner with zero-to-little programming background, I would recommend you to get the screencast, at least.

Why?

Two things; first it goes beyond what the online book has to offer. You’ll end up learn more even if you can absorb 100% of the knowledge from the online book. In the screencast, you’ll get to see the real life process of programming world. In specific, that means you get to see how Mike makes those human errors and how he tackles unexpected challenges that pops up while going through the lessons. These instances are not captured inside the online books (not that it was intentionally left out, but that’s exactly the whole point of screencasting), and best learned from the screencast.

Second, it speeds up your learning process. It’s much more faster and easier to learn from the screencast. So in the screencast, you’ll get to see how Mike goes through the lessons like you’ll see in the book, but at the same time, you get to see how he uses keyboard shortcuts (say CMD + K to clear out the Terminal page). I have zero background in programming which means I have never use Terminal app before, so those were really helpful. You’ll learn a lot of these not just from this screencast but any other screencast as well. These kind of little tricks usually come in small chucks and you’ll love it because it saves you a lot of time when you learn and use them in real context. There will be a keystroke logger on the screen too, which is very helpful. You’ll learn those shortcuts in an instant. (While he was going through the lessons and do those little neat tricks, trust me you’ll find yourself asking, “Whoa wait, what was that?” or “Whoaa wait a minute, how did you..?”. That KeyCastr thing will save your life.)

I documented my whole learning process from the tutorial by jotting down the key points I learned from each chapter. I also keep track of all technical issues and problems that I encountered, together with the solutions that I used to overcome them. You will not necessarily stumbled upon the same issues that I had, but if you do, I hope this will help.

If you ever find this post helpful and decided to get the screencast, get it from this link if you feel like donating, but without having to spend a cent.

Skip to: Lesson 1 - From zero to deploy Lesson 2 - A demo app Lesson 3 - Mostly static pages Lesson 4 - Rails-flavored Ruby Lesson 5 - Filling in the layout Lesson 6 - Modeling and viewing users: part 1 Lesson 7 - Modeling and viewing users: part 2 Lesson 8 - Sign up page Lesson 9 - Sign in, Sign out Lesson 10 - Updating, showing, and deleting users Lesson 11 - User microposts Lesson 12 - Following users Lesson 13 - Rails 3.1

My local environment setup

I believe this info is fairly important because different system has different setup and version installed. Some times if you’re unlucky, you’ll hit walls and the solutions are nowhere near the screencast or online book. So when that happened, the best place to look up for help and answer would be Google and StackOverFlow. It happened to me few times, and mostly the answers are already there for you to dig up.

Environment Setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$ rvm -v
rvm 1.6.32 by Wayne E. Seguin ([email protected]) [https://rvm.beginrescueend.com/]

$ rails -vRails 3.0.9

$ ruby -vruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

$ bundle show autotest
~/.rvm/gems/[email protected]/gems/autotest-4.4.6

$ bundle show spork
~/.rvm/gems/[email protected]/gems/spork-0.9.0.rc8

$ gem list

*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (3.0.9)
actionpack (3.0.9)
activemodel (3.0.9)
activerecord (3.0.9)
activeresource (3.0.9)
activesupport (3.0.9)
addressable (2.2.6)
annotate (2.4.0)
arel (2.0.10)
autotest (4.4.6, 4.4.5, 4.3.2)
autotest-fsevent (0.2.4, 0.2.2)
autotest-growl (0.2.9, 0.2.4)
autotest-rails-pure (4.1.2, 4.1.0)
builder (2.1.2)
bundler (1.0.15)
diff-lcs (1.1.3)
erubis (2.6.6)
heroku (2.4.0)
i18n (0.5.0)
launchy (2.0.5)
mail (2.2.19)
mime-types (1.16)
nokogiri (1.5.0)
polyglot (0.3.2)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.9)
railties (3.0.9)
rake (0.9.2 ruby)
rdoc (3.9.4, 3.9.2, 3.9.1)
rest-client (1.6.3)
rspec (2.6.0, 2.5.0, 2.0.1, 2.0.0.beta.18)
rspec-core (2.6.4, 2.5.2, 2.0.1, 2.0.0.beta.18)
rspec-expectations (2.6.0, 2.5.0, 2.0.1, 2.0.0.beta.18)
rspec-mocks (2.6.0, 2.5.0, 2.0.1, 2.0.0.beta.18)
rspec-rails (2.6.1, 2.5.0, 2.0.1, 2.0.0.beta.18)
spork (0.9.0.rc8, 0.8.4)
sqlite3 (1.3.4, 1.3.3)
sqlite3-ruby (1.3.2, 1.2.5)
sys-uname (0.8.5)
term-ansicolor (1.0.6)
thor (0.14.6)
treetop (1.4.10)
tzinfo (0.3.29)
webrat (0.7.3, 0.7.1)
ZenTest (4.6.2)

$ bundle show
Gems included by the bundle:

  * ZenTest (4.6.2)
  * abstract (1.0.0)
  * actionmailer (3.0.9)
  * actionpack (3.0.9)
  * activemodel (3.0.9)
  * activerecord (3.0.9)
  * activeresource (3.0.9)
  * activesupport (3.0.9)
  * arel (2.0.10)
  * autotest (4.4.6)
  * autotest-fsevent (0.2.4)
  * autotest-growl (0.2.9)
  * autotest-rails-pure (4.1.2)
  * builder (2.1.2)
  * bundler (1.0.15)
  * diff-lcs (1.1.3)
  * erubis (2.6.6)
  * i18n (0.5.0)
  * mail (2.2.19)
  * mime-types (1.16)
  * nokogiri (1.5.0)
  * polyglot (0.3.2)
  * rack (1.2.3)
  * rack-mount (0.6.14)
  * rack-test (0.5.7)
  * rails (3.0.9)
  * railties (3.0.9)
  * rake (0.9.2)
  * rdoc (3.9.4)
  * rspec (2.6.0)
  * rspec-core (2.6.4)
  * rspec-expectations (2.6.0)
  * rspec-mocks (2.6.0)
  * rspec-rails (2.0.0.beta.18)
  * spork (0.9.0.rc8)
  * sqlite3 (1.3.3)
  * sys-uname (0.8.5)
  * thor (0.14.6)
  * treetop (1.4.10)
  * tzinfo (0.3.29)
  * webrat (0.7.3)

Lesson 1 - From zero to deploy

  • Learn how to create Rails files and install gems from terminal.
  • Configure and go through basic git commands from terminal.
  • Don’t ‘sudo’ when use rvm.
  • Create and configure heroku. Push local to heroku using git push.

Issue: Can’t get past chapter 1 screencast for ‘bundle install’ Solution: Inside Gemfile;

In Gemfile Reference Link
1
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'

Issue: Can’t install heroku. No permission error for current user. Solution: Set permission for .rvm to current user solves the issue. In terminal,

In Terminal
1
$ sudo chown -R $USER:staff ~/.rvm

Back to top

Lesson 2 - A demo app

  • Modify .bash_profile (for Mac only) to modify terminal appearance (PS1) just like in screencast [~]$.
  • Fixed heroku issue related to rake:migrate.
  • Created StackOverflow account to keep track helpful and related answers.

Issue: Heroku issue (rake aborted! uninitialized constant Rake::DSL)

uninitalized constant Rake::DSL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(in /app)
rake aborted!
uninitialized constant Rake::DSL
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:6:in `<module:Rake>'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:3:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `require'
/app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks/documentation.rake:2:in `require'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks/documentation.rake:2:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks.rb:15:in `load'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks.rb:15:in `block in <top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks.rb:6:in `each'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/tasks.rb:6:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:215:in `require'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:215:in `initialize_tasks'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:139:in `load_tasks'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
/app/Rakefile:7:in `<top (required)>'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/ruby1.9.2/bin/rake:31:in `<main>'

Solution: There were two solutions. Use an older version of rake or require DSL:

In Rakefile Help link
1
require 'rake/dsl_definition'

Back to top

Lesson 3

  • Learn about TDD (test-driven development)
  • Setup ‘autotest’ and ‘spork’
  • Run autotest with bundle exec
  • Run spork properly

In Lesson 3, I hit few walls in setting up both Spork and autotest. If you had a smooth experience following the tutorial then feel free to skip this part. I believe these are strictly unique issues caused by the complexations of the system and gem files versions. I also went through Lesson 3a screencast.

Issue: Somehow autotest folder wasn’t created, like the one in screencast. Followed every step including generate rspec:install from tutorial.

Solution: Started with below code (noticed the ‘-T’) so test directory is created;

Inside Terminal
1
2
3
cd ~/rails_projects
$ rails new sample_app -T
$ cd sample_app

Then went for mate Gemfile (as always, the one in screencast will messed up heroku, so used the one below)

In Gemfile
1
2
gem 'rails', '3.0.x'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'

Issue: Spork doesn’t work up to spork –bootstrap part of the tutorial. Solution: The last part of the hack needed to get Spork to run with Rails 3. Do mate config/application.rb inside your Terminal.

Open config/application.rb ref link
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require File.expand_path('../boot', __FILE__)

require 'rails/all'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module SampleApp
  class Application < Rails::Application
    .
    .
    .
    ### Part of a Spork hack. See http://bit.ly/arY19y
    if Rails.env.test?
      initializer :after => :initialize_dependency_mechanism do
        # Work around initializer in railties/lib/rails/application/bootstrap.rb
        ActiveSupport::Dependencies.mechanism = :load
        end
      end
    end
  end

Back to top

Lesson 4 - Rails-flavored Ruby

  • Rails c or console inside Terminal (run local Rails server at localhost:3000)
  • Ruby’s basic like object, string, arrays, method, range, function. boolean empty? nil?
  • Ruby API at http://ruby-doc.org/core/
  • Enumerable
  • Accumulate is called inject in Ruby
  • String & Symbol (:symbol) & Hash {} & Class (something.class)
  • Interpolate “#{something}”
  • Constructor
  • Action
  • Added title helper and blueprint.css

In this lesson Mike went through a lot of Ruby basics, as you can see on learning points above. Don’t get overwhelmed, which exactly what you’ll experience going through this chapter. It’s called programming language for a reason; the identifiers like what’s a block of code? or what’s a method? will keep popping up and get you all mixed up. Don’t worry too much about it now, instead stay focus and give it 100% of your attention. You’ll get more familiar with the language as you work more on it. I did the tutorial twice and surprisingly, the first-time experience did help me recalled all these and made so much sense compared to the first time I went through it.

I don’t have any issue with this chapter and most of my notes are images about inheritance hierrachy (which you can find on the online tutorial book).

Back to top

Lesson 5 - Filling in the layout

  • git checkout -b (create new branch)
  • html5shiv for IE
  • name route
  • integration tests
  • match function to route from “pages/home” to “/”
  • git commit –amend
  • add Users controller and signup integration tests
  • Partials

Again, chapter 5 was a breeze and most of my notes are images/screenshots of related files covered in Lesson 5. As mentioned earlier, stay focus on the tutorial as this post is meant to get absolute beginners to break through their walls. I know what it’s like being distracted from doing what matters, so I don’t want this to be a distraction.

Back to top

Lesson 6 - Modeling and viewing users: part 1

  • user data model
  • learn ActiveRecord (default Rails library for user database)
  • DDL (stands for database definition language)
  • Users controller (plural) while User model (singular)
  • SQLite database browser
  • tail -f log/development.log
  • single or double quotes matter in interpolation
  • validation test inside ‘spec/models’
  • length and format validation
  • regex on Rubular.com
  • database indices
  • “params for debug” blocks for test environment

If you haven’t create a learning journal up to this point, you better start now. It could be in any form you prefer; blog post, notepad, Evernote. I use Evernote for note taking and believe it’s the best tool for it. Going through my screenshots of related file for specific chapter helps me recall and remember what I was doing within specific chapter.

Back to top

Lesson 7 - Modeling and viewing users: part 2

  • insecure password
  • password encryption
  • secure password
  • salt and rainbow attack
  • between “self” user object & “self” class methods
  • Factory Girl (factories.rb)
  • stubbing ( RSpec’s stub! method)
  • gravatar
  • user sidebar
  • heroku open, logs, rake db:migrate & console

I had no hiccup or technical issue in this chapter.

Back to top

Lesson 8 - Sign up page

  • partial always start with underscore “_something”
  • Sign up error messages
  • Sign up success
  • RSpec integration tests

Chapter 8 was a smooth ride for me. No bumps at all. Stay focus.

Back to top

Lesson 9 - Sign in, Sign out

  • sessions
  • sign in failure
  • flash.now
  • completed create action
  • remember me
  • current user
  • setter method & getter method
  • ternary operator?
  • def signed_in? in sessions_helper.rb
  • sign out + destroy sessions
  • sign in upon signup
  • changing the layout links
  • sign in/out integration tests

Lesson 9 was conflict-free. Spent some time learning about RESTful architecture.

Back to top

Lesson 10 - Updating, showing and deleting

  • protecting “Edit user” page
  • diff in screencast & online tutorial books (Listing 10.8)
  • protecting edit and update with a before filter
  • rails generate integration_test friendly_forwarding
  • require the right users2
  • show all users
  • will_paginate & faker
  • destroying users

Finally some issues popped up! It’s weird to get excited about it but there’s no point of walking through a tutorial without any takeaway points to learn from. So I ran into these;

Issue: Will_paginate rspec test error Solution: Add this into your Gemfile

Inside Gemfile ref link
1
gem 'will_paginate', '3.0.pre2'

Don’t forget to run bundle install after adding it inside Gemfile.

Issue: Undefined _selector title rspec error Solution: I believe this was updated inside the online tutorial book

Inside Gemfile ref link
1
2
3
4
5
6
7
8
group :development do
  gem 'rspec-rails', '2.0.0.beta.18'
end

group :test do
  gem 'rspec-rails', '2.0.0.beta.18'
  gem 'spork', '0.8.4'
end

Back to top

Lesson 11 - User microposts

  • micropost associations
  • micropost validations - end of micropost model
  • showing microposts
  • sample microposts
  • user feeds
  • delete link with destroy action

Issue: Change rspec-rails gem in Gemfile Solution:

on rails inside Gemfile
1
2
3
gem 'rspec-rails', '2.6.1'

gem 'rspec-rails', '2.0.0.beta.18'

Issue: Rspec autotest error for not found Factory :micropost Solution: In factories.rb, change Factory.sequence to

on rails factories.rb
1
2
3
# replace Factory.sequence to

Factory.define

Back to top

Lesson 12 - Following users

  • relationship model
  • user/relationship associations
  • following
  • followers
  • sample_data.rake
  • following/followers pages
  • follow button with Ajax
  • status feed
  • scopes,subselects and a lambda

Back to top

Lesson 13 - Rails 3.1

  • bundle exec rake spec
  • cat Gemfile inside Terminal (whatever that means)
  • mate ~/.gemrc will skip Rails docs (gem: –no-ri –no-rdoc)
  • transfer and run rspec files over one-by-one; starting with spec/
  • new way to do migration (rake db:migrate) in 3.1
  • test failed on rspec spec/request (remove the landing page index.html will make it green)
  • cp ../sample_app/.gitignore .
  • blueprint (since it was from external source) exported into vendor/assets/stylesheets folder
  • don’t forget to transfer sample_data file in lib/
  • “Lucas Heller” case; change create.js.erb & destroy.js.erb to jQuery syntax (.update to .html)
  • git checkout -b rails_3_1 then cp -r ../sample_app_3_1/ .*

Some issues I had in Lesson 13 were expected issues, which are covered in this chapter. This chapter is a good example of why you should get the screencast. You’ll get to see the real process of when something went wrong, how Mike handled them and what steps he took to overcome them. Another thing to take note is if you find yourself copy-pasting from the book a lot, you might want to type down everything manually. So that way, you’ll gain more knowledge than just walking through a tutorial without much focus and attention.

Back to top

Comments