Ruby on Rails Notes...

Documentation began: April, 2009

Using:

Ubuntu 8.04
Ruby version 1.8.6
Gem version 1.3.2
Rails version 2.3.2

Commands to learn your versions

//for ruby
ruby -v
//for gem
gem -v
//for rails
rails -v

I checked out RailsSpace from the Bellingham Linux Users Group's Library to get me started down the RoR road (for reals this time). Visiting the BLUG Library was a pleasant surprise. There are quite a few useful books on their shelf available for free checkout, just like your local library, but for techies. Very cool.

Installing Ruby & Rails

Getting started always seems to most time consuming ordeal. Here are the steps that got me going. Installed in an Ubuntu VM. Steps from RailsSpace book, plus one added step needed to get the mongrel server running.
Installed ruby with command

sudo apt-get install ruby irb rdoc

Downloaded RubyGems .tgz file from here, extracted, then executed below command from within the source directory

ruby setup.rb

lastly, installed Rails

gem install rails --include-dependencies

Creating a new project with `rails my_test_app_name` command worked. But mongrel would not start when doing...

cd my_test_app_name
ruby script/server

Received an error that began like this:

=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:269:in 
`require_frameworks': no such file to load -- net/https (RuntimeError)
	from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:134:in `process'

One more package needed installing.

sudo apt-get install libopenssl-ruby1.8

Ran the ruby script/server command again, mongrel started up, then entered http://localhost:3000/ in the browser and successfully got the Ruby on Rails Welcome page.

RadRails for the IDE

Steps to setting up RadRails

  • Download Aptana Studio.
  • Unzip into your home directory.
  • Found some excellently easy to follow install Aptana instructions at MakeTechEasier (for Ubuntu).
  • Now install the RadRails plugin: Run Aptana > on the My Aptana tab click Plugins icon > under Platform click the RadRails "Get It" link and follow the wizard.

RailsSpace Issues

RailsSpace was written for Rails v1.2.3 - Important to note because, as I've learned, through some time consuming ordeals, a few things have changed in Rails 2. This is not complaining cause I've rather enjoyed the research/learning experience.

Default File Extension

This was a quick learn. The Default "views" file extension in Rails 2 is .html.erb
The old default was .rhtml

Rails Testing

Attempting just to get started with the 'testing' chapter was a day plus effort.
Using this command to run my test...

ruby test/functionals/site_controller_test.rb

...kept returning this error

test/functional/site_controller_test.rb:1:in `require': no such file to load -- test_helper (LoadError)
	from test/functional/site_controller_test.rb:1

Noticed the default *_controller_test.rb files created by my project were different from the book, but that wasn't the problem. You must be in the test directory, then run the same command, minus the test/ part, like so...

// starting in project directory, change directory to test
cd test
// now run command
ruby functional/site_controller_test.rb

It works. And yes, it took me a whole day to figure that out.

During the 'research problem' process, learned these commands will also run tests.

// Uses rake to run all test files in the functional directory
rake test:functionals
// Uses rake to run test on a specific file
rake test:functionals TEST="test/functional/site_controller_test.rb"

RubyGuides Testing, a good online resource regarding testing in Rails 2.
Rake commands sub heading from above link to prepare app. for testing.

Testing Min and Max Length

An answer to another "stopped in my tracks" moment.

Was receiving the following failure when running the `rake test:units` command

.DEPRECATION WARNING: ActiveRecord::Errors.default_error_messages has been deprecated. 
Please use I18n.translate('activerecord.errors.messages').. 
(called from default_error_messages at 
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:24)
.
Finished in 0.281394 seconds.

  1) Failure:
test_screen_name_minimum_length(UserTest) [/test/unit/user_test.rb:50]:
<"is too short (minimum is {{count}} characters)"> expected but was
<"is too short (minimum is 4 characters)">.

4 tests, 10 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"/home/james/rails_spac...]

Traced the deprecated and failure messages to these 2 lines of code.

// This triggered the deprecated message
@error_messages = ActiveRecord::Errors.default_error_messages
.
.
.
// This line caused the failure
correct_error_message = sprintf(@error_messages[:too_short], min_length)

That was the former (deprecated) way of doing it. But Rails now supports i18n internationalization, and here's the new fashion. I replaced the above 2 lines of code with these

@error_messages = I18n.translate('activerecord.errors.messages')
.
.
.
correct_error_message = I18n.translate"activerecord.errors.messages.too_short", :count => min_length

// And this line now runs failure free
assert_equal correct_error_message, user.errors.on(:screen_name)

To test max_length, replace too_short above with too_long and min_length with max_length.

Testing Cookies

Got hung up in a circle of frustration while testing cookies via the RailsSpace way. This is for code listings 7.21 and 7.22 (p. 206). The following function is called by test_login_success

def cookie_value(symbol)
	cookies[symbol.to_s].value.first
end

And I kept getting this error

1) Error:
test_login_success(UserControllerTest):
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.value
    functional/user_controller_test.rb:272:in `cookie_value'
    functional/user_controller_test.rb:136:in `test_login_success'

Tested various ways, and used "puts" to display the cookie values in the test output.
.value.first seemed to be the root of the problem

# using .value.first
puts cookies[:remember_me.to_s].value.first
# would throw this error
NoMethodError: undefined method `value' for "1":String

# removing .value.first and no more errors
puts cookies[:remember_me.to_s]
puts cookies[:authorization_token.to_s]

Made the following change inside cookie_value()

def cookie_value(symbol)
    cookies[symbol.to_s]
end

I knew there would still be a problem when trying to test the cookie's expiration date, because if I can't access value via .value, how am I going to access expires via .expires? After still more research, I've come to the conclusion .value and .expires are no longer "the way", with this change coming into effect at Rails version 2.3. The answer finally becoming clear after reading the last 2 paragraphs of this enlightening post on zargony.com. The cookie accessor only returns the cookie's "value", which it now does by default. No further attributes are parsed. Meaning "expires" is not testable, as least not easily.

Top of page