Archive for July 2008
If you ever tried running your tests and find yourself getting the error:
your_controller_test.rb:3:in `require': no such file to load -- test_helper (LoadError)
it’s probably because the generated require call,
require test_helper is not being properly recognized. Instead of the default
require 'test_helper', replace it with:
require File.dirname(__FILE__) + '/../test_helper'
If you’re doing functional testing, make sure that the controller being tested is also required (e.g. add the line
require 'your_controller' on top). Be sure that you also have installed the package
libtest-unit-ruby (if using Linux), before testing.
Also, thanks to Jeff Balogh, make sure that you append test_ on all your test cases to make sure that they actually run (I know it’s obvious, but it’s nice to be reminded once in a while).
On database constraints:
If you want to enforce foreign key constraints, remember to create the required indexes first. ActiveRecord Migration supports this using add_index. This only applies if the column being referred to is not a primary key, if it is then there’s no need to create the index. Once this is set, you can now enforce foreign keys using ActiveRecord Migrations by implementing this.
On implementing sessions:
By default, the rails session handler stores session information on a file in the server. This can cause scalability issues in the long run if file space runs out or when using multiple servers. Thus, it’s recommended to use
:active_record_store instead. To do this make sure that you do the following:
rake db:sessions:createto add a create table migration for sessions
rake db:migrateto apply the new schema
- change session store to active session store by adding this line to
config.action_controller.session_store = :active_record_store
- Make sure that you comment out or add this line to your ApplicationController:
protect_from_forgery :secret => <32_hex_digit_secret_key>
Here are also some helpful links regarding sessions and implementing it:
…pretending to be a RoR programmer, if:
- You keep typing “nill” (null) instead of nil
- You think private and protected keywords apply only to the method, thus spending the whole afternoon scratching your head and asking why your action is not being found.
- You keep using extends on modules and don’t know that there’s an include as well (and don’t know the difference between them).
- You feel uneasy comparing Strings with == and uses eql? instead (even if they’re the same)
- You think ruby symbols is just Matz’s way of messing with your mind (like Matrix did)
- You think Rails is WebObjects on steriods…and acid at the same time 🙂
Update: March 13, 2009
I’ve tried installing rails again from scratch, from a newly installed Ubuntu 8.10 and I got a “no ruby-full package” error when I tried to execute the install command
sudo apt-get install ruby-full rubygems build-essential
If this happens, you can enable external package repositories by going to System -> Administration -> Software Sources. Then you can go to Third-Party Software tab and select both sources (‘http://archive.canonical.com/ubuntu intrepid’ as of Ubuntu 8.10) and do a
sudo apt-get update after on the command line. Once update is done, installing ruby-full package should now work.
Here, I outline the steps I took to get started with Ruby on Rails development on Ubuntu 8.04
1. Install ruby
To install ruby (1.8.6) and rubygems, simply type:
sudo apt-get install ruby-full rubygems build-essential
2. Update gem
When installing rails, make sure that you let gems manage it rather than apt-get, otherwise, “gem and apt-get will start fighting like Spock and Captain Kirk in Amok Time.” Thus, we need to make sure rubygems is updated using the command
sudo gem update (for gems 0.9.4)
Now, you might or might not encounter this but when I did this on Ubuntu 8.04, I got the following error:
Bulk updating Gem source index for: http://gems.rubyforge.org
Attempting remote update of rubygems-update
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find rubygems-update (> 0) in any repository
Asking the all-knowing Google lead me to this fix. You apparently need to remove the local source_cache file found in the GEM PATH. You can get this path by running the command
gem env, giving you this result:
$ gem env
- VERSION: 0.9.4 (0.9.4)
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- GEM PATH:
- REMOTE SOURCES:
Once you get your GEM PATH (in my case /var/lib/gems/1.8), then you need to remove the local cache by typing:
sudo rm -rf /var/lib/gems/1.8/source_cache
and continue to update gem.
3. Install rails
Now that you have gem updated, you can then confidently install rails and its dependencies by typing:
sudo gem install rails --include-dependencies
Make sure that you also add the gem bin path to your path. If you’re using bash, then add the following line to your .bashrc, replacing <GEM_PATH> with your gem path found by typing gem env (don’t forget to restart terminal):
Verify installation by typing,
rails -v and get the following (or similar) response:
4. Install a Database
Since I already have a MySQL server, I wanted to work with it instead of the default SQLite database for Rails. This means installing the MySQL driver for rails and typing the command:
sudo apt-get install libmysql-ruby mysql-server
You can leave out the mysql-server if you already have the MySQL server installed.
5. Install a Ruby on Rails server
Reading some of the posts, I found that there are two most popular servers to use for RoR apps: Mongrel and Webrick. Webrick I believe comes with the rails installation, but there’s huge evidence that Mongrel might be better than Webrick. Using the steps found here, I installed Mongrel on Ubuntu by typing:
sudo gem install mongrel --include-dependencies
You might be given options to select versions. In my case, I just selected the latest version of mongrel (1.1.5) and fastthread (1.0.1) for ruby.
6. Create sample application
To make sure everything works, we can create a sample rails application. Start by typing
rails cookbook -d mysql if you’re using MySQL, otherwise leave the -d flag out to use SQLite instead. The database configuration assumes that you have a MySQL user called root, with the password disabled. If this is not the case, then you might need to edit the config file cookbook/config/database.yml and make appropriate changes under development. To make sure the database also works, we need to create a sample application that uses our database. To do this, just type in console under the cookbook directory:
ruby script/generate scaffold recipe title:string description:text category:string
mysql -u root --password=<your_password> -e "CREATE DATABASE cookbook_development"
Once that’s done, just execute
mongrel_rails start under the cookbook directory, fire up your browser to http://localhost:3000/recipes (note that it’s plural) and you should see your cookbook web application staring back at you.
Note: If you get a
CGI::Session::CookieStore::TamperedWithCookie error when hitting the recipes page, just hit browser refresh and that should be fixed (not sure why it does that sometimes).
7. Find a good Ruby on Rails IDE (no, this is not optional)
Now the hard part of all this is getting settled into a familiar development environment. There are some good discussions out there on what people use as RoR IDEs. Since I don’t have a week to throw away learning emacs, I’ve decided to start with something familiar, Eclipse. The only decent Eclipse-based IDE out there is RadRails, but installing the standalone verion, I found that it was too alpha for me at this stage (crashes frequently, unresponsive, and can’t find my rails installation). So I’m settling right now with the Rails plug-in for NetBeans and so far it’s been good for me, except for the occasional GUI lag that I’m getting. I’ve also heard good things about jEdit, but without the debugger support, it’s not technically an IDE for me.
It’s officially over. With five weeks left, I’m done doing WebObjects development for Web-CAT, and switching to a new project. The powers-that-be have decided to focus on OLM instead and supplementing it with features that it lacks. For now, this would involve taking over student submissions and group associations that would work in parallel with OLM. It’s pretty rare to start a project from scratch, so I really want to tread carefully with design and testing on the forefront, especially that this is my first time using RoR as well.
Developing this on RoR would serve two purposes. Since I only have five weeks left, we need some rapid prototyping environment to put this all together and have something to show at the end of summer to see if our understanding of submission and group workflow does indeed work. The second purpose is to quickly assess if RoR is indeed a suitable platform for us given the requirement of plugability for the features that we want. If all of this works by the end of summer, we could then perhaps start porting OLM bit by bit over to the rails application.
It’s a bittersweet goodbye, but we learn what we can from our mistakes and hope to do better the second time around.