running rspec in aptana rad rails
Problem: I wanted to run a rspec in Aptana RadRails as a Test::Unit Test (Alt+Shift+X U) I have a spec/controller/my_controller_spec.rb file generated by rspec_controller generator. While running it in Aptana I got:
./spec/controllers/my_controller_spec.rb:1:in `require': no such file to load -- spec_helper (LoadError)
The solution is to change what the generator generated. Replace in the file:
require 'spec_helper'
with:
require 'spec/spec_helper'
This should help. Running this rspec through rake (rake spec) should work still as well.
problem starting sphinx with rials
Problem:
When doing:
rake thinking_sphinx:index
you get:
undefined method `[]' for false:FalseClass
Solution: remove the empty config/schema.yml file.
cucumber with email_spec problem
Problem: using email_spec in cucumber with rails 2.3.5 you get:
Using the default profile... uninitialized constant EmailSpec (NameError) /fooProject/vendor/rails/activesupport/lib/active_support/dependencies.rb:443:in `load_missing_constant' /fooProject/vendor/rails/activesupport/lib/active_support/dependencies.rb:80:in `const_missing' /fooProject/vendor/rails/activesupport/lib/active_support/dependencies.rb:92:in `const_missing' /fooProject/vendor/gems/email_spec-0.6.2/lib/email_spec/cucumber.rb:25 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require' (...)
Solution: include the following TWO lines in your features/support/env.rb (not as the documentation says - only the second one :/)
require 'email_spec' require 'email_spec/cucumber'
cucumber vs webrat vs current_user from AuthenticatedSystem
Problem: when using current_user in step definitions you get:
undefined method `current_user' for #<ActionController::Integration::Session:0xb66b25f4>(NoMethodError)
Solution: add the below line at the end of “features/support/env.rb”
ApplicationController.send(:public, :current_user)
And then use it as in the sample below:
Then /^I should be logged in as (.*)$/ do |login| controller.current_user.login.should == login end
rails 2.3 testing application controller
I have some stuff in ApplicationController that need testing. I found a simple way to test those methods, sample (test/functional/application_controller_test.rb):
require File.dirname(__FILE__) + '/../test_helper'
class ApplicationControllerTest < ActionController::TestCase
def setup
@controller = ApplicationController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
test "foo test" do
bar_result = @controller.foo_method
assert bar_result
end
end
failing to run rails test in aptana eclipse
Problem:
File to launch does not exist: '/home/wojtek/workspace_galileo/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb'
Solution: create a new workspace from beginning.
rails custom validation messages i18n
I did not find on the net how to override the “{attribute} {message}” format of validation error messages. It is no use in my language. Sometimes I need a different order or specific message. The way I managed to do it using rails 2.3.4 is:
- use validation message in model, for example:
validates_presence_of :login, :message => :login_blank
- Define the message in your locale in the yml, (en.yml, pl.yml or whatever yours is) under the tree (this example is for “pl” locale):
pl:
(...)
activerecord:
(...)
errors:
(...)
full_messages:
login_blank: "My custom message hahaha!"
(...)
I found this by looking at: RAILS_HOME/activerecord/lib/active_record/validations.rb and the comment to generate_full_message method
dependant dropdowns (select menus) using rails
There are multiple solutions on the net for dynamic select menus. By saying “dynamic menus” I mean that we have a form with two select dropdowns. When we change the first one, the content of the second one is updated.
The most promising that I have found are:
- http://railscasts.com/episodes/88-dynamic-select-menus
This one includes writing JS code. I don’t like writing JS code.
- http://github.com/splendeo/dependent_select
This one does not use AJAX. It generates the whole thing in JS and sends it to the client. Can be useful when dealing with not big data.
- http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails/
A very nice one but in my opinion a bit complicated for a beginner.
I had to develop my own solution. This is what I came up with. Lets assume we have a form for Foo model that has a select dropdown with Bar1 model and select dropdown with Bar2 model, that is dependent on Bar1 current selection.
- In the Foo model form, add a field observer:
<%= observe_field :foo_box1_id, :url => { :action => :box2_select_box },
:update => :foo_box2_id,
:with => :box1_id
%>
- In the FooController add:
def box2_select_box
@box1 = Box1.find(params[:box1_id]) unless params[:box1_id].empty?
render :layout => false
end
- In the view for the above action (box2_select_box.html.erb) put:
<%= if @box1 options_from_collection_for_select(@box1.box2s, :id, :name) else options_for_select([["Nothing to select", ""]]) end %>
No JS code writing. Working with AJAX. Simple.
EDIT:
In reply to Rob Bean’s comment (thanks!) I attach a full rails project with a sample.
The “@box1.box2s” comes from models association. box1 has_many box2s (see box1.rb)
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.