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
rcov running cucumber/rspec problem
Problem: while running this (I got this rake task from http://gist.github.com/231022):
rake rcov:cucumber
/home/xyz/workspace/fooProject/vendor/gems/rspec-1.3.0/lib/spec/runner.rb:48:in `exit': can't convert Object into Integer (TypeError)
from /home/xyz/workspace/fooProject/vendor/gems/rspec-1.3.0/lib/spec/runner.rb:48:in `autorun'
Solution: replace in that file the method (workspace/fooProject/vendor/gems/rspec-1.3.0/lib/spec/runner.rb:48)
def autorun # :nodoc:
at_exit {exit run unless $!}
end
with:
def autorun # :nodoc:
at_exit {run unless $!}
end
I did not go into the details of the why run returns an Object instead of Integer, but I do not need to. I do not need the exit status at the moment. The only thing I am interested in is that rcov generates the coverage results.
ubuntu xml gem instal problem
Problem:
libxslt is missing. try 'port install libxslt' or 'yum install libxslt-devel' *** 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 libxslt1-dev
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)