Posts Tagged ‘Ruby/RoR’

RSpec: Behaviour-Driven Development tool for Ruby

A quick overview of the Test-driven developmen...

Image via Wikipedia

RSpec is a Behaviour-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

via RSpec.info: Home.

`require’: no such file to load — readline (LoadError)

I got the following error when trying to start the ‘rails console’ in a Rails3/Ruby 1.9.2 project:

`require’: no such file to load — readline (LoadError)

The problem was that readline was not installed properly. To install it, I needed to go into the ruby sources. In my case they were located in the directory /use/local/src/ruby-1.9.2-p0/ext/readline. I changed to it an built and installed realdline via

ruby extconf.rb
make
sudo make install

Creation of nested models in Rails3 failed with HashWithIndifferentAccess error

I just tried to create a Rails model which contained nested other models and got an HashWithIndifferentAccess error. I used accepts_nested_attributes_for in the parent model. To solve this problem the solution was to add an attr_accessible :MODELNAME_attributes for the child-model (MODELNAME) into the parent model. E.g.

Parent model

class ParentModel < ActiveRecord::Base
 has_one :child_model
 attr_accessible :child_model_attributes
end

Autoload modules in Rails 3

In Rails 3 the modules in lib/ are not loaded automatically. You need to a a one liner to the application.rb:

config.autoload_paths += %W(#{config.root}/lib)

uninitialized constant MysqlCompat::MysqlRes

I just had the following problem when trying to start a Ruby on Rails application on a fresh installation of Mac OS X Snow Leopard after I installed the mysql gem via gem install mysql:

uninitialized constant MysqlCompat::MysqlRes

I searched a bit and found a solution here. It was a problem with a buggy version of  the Mysql driver. So you need to use a different version when installing the mysql gem. So first uninstall the existing gem via

gem uninstall mysql

and reinstall it via

export ARCHFLAGS="-arch i386 -arch x86_64" ;sudo gem install --no-rdoc --no-ri  -v=2.7 mysql -- --with-mysql-dir=/usr/local/mysql  --with-mysql-config=/usr/local/mysql/bin/mysql_config

xss_terminate – protect your Ruby on Rails code from XSS

Just found this little plugin which is maybe interesting to protect against Cross-site scripting in Ruby on Rails.

xss_terminate is a plugin in that makes stripping and sanitizing HTML stupid-simple. Install and forget. And forget about forgetting to h your output, because you won‘t need to anymore.

via xssterminate – Project Hosting on Google Code.

Retrieve IP address from host name using Ruby

I just needed to retrieve the IP address for a given host name for a Ruby project. What helped me to achieve this goal was the Socket class. In this example I want to get the IP address for google.com:

>> Socket::getaddrinfo('google.com', 'www', nil, Socket::SOCK_STREAM)[0][3]
=> "209.85.129.147"