rails integration tests hardcoded routes problem
We had a problem with hard-coded routes in integration tests while using mixed RESTful and default routing. For example we can have in a integration test:
test "my sample test" do get 'foo_controler/bar_action' end
This looks not too nice, a string-hard-coded route to controller and its action.
The quick and fair solution we found was this one: First of all: do not use default routes, use RESTful routing only (see http://www.slideshare.net/ihower/rails-best-practices)
So we comment these things in routes.rb:
#map.connect ':controller/:action/:id' #map.connect ':controller/:action/:id.:format'
Second of all: use named routes instead of the default ones, so we add to routes.rb:
map.foo_controler 'foo_controler/:action/:id', :controller => :foo_controler
And change the integration test to:
test "my sample test" do get foo_controler_path(:action => :bar_action) end
Let me know what you think about that.
EDIT:
I am still learning much rails.
I came across another problem and found the “:only” parameter for the map.resources. This means that one should not use named routes. Only RESTfull routes should be used and the inclusion of the methods you want to implements is done by “:only” as the API says
This example is copied form the API reference:
map.resources :posts, :only => [:index, :show] do |post|
post.resources :comments, :except => [:update, :destroy]
end
Example of adding new, non-standard, routes:
map.resources :users, :collection => {
:show_profile => :get,
:edit_profile => :get,
:update_profile => :post
}
So the above integration test will look diferent. The helper methods can be found by listing the routes with “rake routes”.
disabling color sql logs in rails
I use Aptana IDE (Eclipse) and the escape characters do not look nice in the default console. The way to prevent this is to do:
ActiveRecord::Base.colorize_logging = false
for example in your development.rb file.
I mean rails 2.3.4. If this does not work for you (other version of rails or whatever) just look at the format_log_entry(message, dump = nil) method of class AbstractAdapter in your rails environment RAILS_HOME/active_record/connection_adapters/abstract_adapter or something.
ubuntu ruby mysql gem installation problem
Problem:
/usr/bin/ruby1.8 extconf.rb install mysql checking for mysql_query() in -lmysqlclient... no checking for main() in -lm... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lz... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lsocket... no checking for mysql_query() in -lmysqlclient... no checking for main() in -lnsl... yes checking for mysql_query() in -lmysqlclient... no *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
Solution:
apt-get install libmysql++-dev
ruby gems repositiory down
Problem: ERROR: http://gems.rubyforge.org/ does not appear to be a repository
Solution: in my case it was that the repository was down. I checked http://gems.rubyforge.org/ and it was not working. So the solution was to wait for it to work again.
The message displayed by gem is very unfortunate, cos it does not say that the repo is down. There are many other reasons for this message to appear probably not associated with the ups and downs of the site.
ruby 1.8.6 in ubuntu 9.04 64 minimal
This was inspired by: ruby-on-rails-development-on-ubuntu-9-04-jaunty-using-ruby-1-8-6 . That unfortunately did not work for me.
Edit the file:
/etc/apt/preferences
Add to it:
Package: ruby Pin: release a=hardy Pin-Priority: 900 Package: ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: rdoc1.8 Pin: release a=hardy Pin-Priority: 900 Package: ri1.8 Pin: release a=hardy Pin-Priority: 900 Package: libgtk2-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libdbd-sqlite3-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libopenssl-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libsqlite3-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: ruby1.8-dev Pin: release a=hardy Pin-Priority: 1001 Package: libdbi-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libatk1-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libpango1-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libatk1-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libgdk-pixbuf2-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libglib2-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: libcairo-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: irb1.8 Pin: release a=hardy Pin-Priority: 900 Package: libreadline-ruby1.8 Pin: release a=hardy Pin-Priority: 900 Package: rails Pin: release a=hardy Pin-Priority: 900 Package: libncurses-ruby1.8 Pin: release a=hardy Pin-Priority: 900
Edit the file:
/etc/apt/sources.list
Add to it:
deb http://gb.archive.ubuntu.com/ubuntu/ hardy restricted main multiverse universe deb-src http://gb.archive.ubuntu.com/ubuntu/ hardy restricted main multiverse universe
Install ruby
apt-get update aptitude install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev
ruby php-like var_dump
Try something like:
p my_variable.inspect
rconv rails rake task test:test:rcov overwrites its output
Problem:
rake test:test:rcov
overwrites its output.
Semi-solution that I use: add to Rakefile
namespace :rcov do
TESTS_DIR = 'test'
Rcov::RcovTask.new(:all) do |rcov|
rcov.pattern = FileList["#{TESTS_DIR}/**/*test*.rb"]
rcov.output_dir = 'coverage'
rcov.verbose = true
rcov.rcov_opts << "--sort coverage"
end
end
Then use the task:
rake rcov:all
rails tests uninitialized constant ActiveRecord
Problem: while running tests you get
uninitialized constant ActiveRecord (NameError)
Solution: in my case it was that I was tired and ran the tests no on the test class bu on the model class! Strange error though.
rails 2.3.2 uninitialized constant ActionView::Helpers::FormHelper
Problem:
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:440:in `load_missing_constant':NameError: uninitialized constant ActionView::Helpers::FormHelper /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant':NameError: uninitialized constant ApplicationController
Solution: for me it was that I did
require "calendar_date_select"
in environment.rb before the config. You have to move that require line to the end of the file.
NotIdentifiedByImageMagickError paperclip rails error
Problem:
Paperclip::NotIdentifiedByImageMagickError in Ebook categoriesController#create is not recognized by the 'identify' command. vendor/plugins/paperclip/lib/paperclip/geometry.rb:24:in `from_file'
Solution: you probably did not specify your form as multipart
(...)
form_for @model_name, :html => { :multipart => true } do |f|
(...)