Monday, July 30, 2007

How to add Rails plugin files to SVN during install

use -x flag. Example:

script/plugin install -x http://svn.techno-weenie.net/projects/plugi
ns/restful_authentication/

Tuesday, July 24, 2007

Recurring Billing in Rails

Research notes:

Dealing with subscriptions payment
Follow-up to subscriptions
Recharging to the Same Credit Card

Sample test case:

def test_reference_purchase
assert response = @gateway.purchase(Money.new(10000), @creditcard, @options)
assert_equal "Approved", response.message
assert response.success?
assert response.test?
assert_not_nil pn_ref = response.authorization

# now another purchase, by reference
assert response = @gateway.purchase(Money.new(10000), pn_ref)
assert_equal "Approved", response.message
assert response.success?
assert response.test?
end

Acts As Billable Plugin

script/plugin install http://source.collectiveidea.com/public/acts_as_billable/trunk/
There’s a start of a sample app (that uses rspec) that goes with it at http://source.collectiveidea.com/public/acts_as_billable/samples/depot

Authorize.Net Automated Recurring Billing (ARB) Example Code

ARG Guide

Tuesday, July 17, 2007

Help in writing tests in Rails

script/console test

It will load the console in the test environment. Very useful to experiment with the code and write tests.

Saturday, July 14, 2007

Railscookbook example error

NameError in User#list_perms

Showing app/views/user/list_perms.rhtml where line #22 raised:

uninitialized constant UserHelper::Permission

create the permission model without migration.

Also the migration for permissions table does not have a id column. This results in a bug where the update does not work for some cases. So I had to add a primary key column to the permissions table. This fixed the buggy update problem.

How to skip migration in Rails

use --skip-migration switch: Ex: script/generate model user --skip-migration

Thursday, July 05, 2007

How to suppress rendering of layout while generating xml in Rails

def my_xml_method
@foo = Foo.find(:all)
render :layout => false
end

If you have render and respond_to in the same method, you will get the dreaded double render error.

Wednesday, July 04, 2007

How to install RubyInline in Mac OS

$ sudo gem install rubyinline
Successfully installed RubyInline-3.6.3
Installing ri documentation for RubyInline-3.6.3...
Installing RDoc documentation for RubyInline-3.6.3...

Port install did not work:

sudo port install rb-rubyinline
---> Fetching autoconf
---> Attempting to fetch autoconf-2.61.tar.bz2 from http://ftp.gnu.org/gnu/autoconf
---> Verifying checksum(s) for autoconf
---> Extracting autoconf
---> Configuring autoconf
---> Building autoconf with target all
---> Staging autoconf into destroot
---> Installing autoconf 2.61_0
---> Activating autoconf 2.61_0
---> Cleaning autoconf
---> Fetching rb-rubygems
---> Attempting to fetch rubygems-0.9.4.tgz from http://rubyforge.org/frs/download.php/20989/
---> Verifying checksum(s) for rb-rubygems
---> Extracting rb-rubygems
---> Configuring rb-rubygems
Error: Target com.apple.configure returned: configure failure: shell command " cd "/opt/local/var/db/dports/build/_opt_local_var_db_dports_sources_rsync.rsync.darwinports.org_dpupdate_dports_ruby_rb-rubygems/work/rubygems-0.9.4" && /opt/local/bin/ruby -rvendor-specific setup.rb config " returned error 127
Command output: sh: line 1: /opt/local/bin/ruby: No such file or directory

Error: The following dependencies failed to build: rb-hoe rb-rake rb-rubygems rb-rubyforge
Error: Status 1 encountered during processing.

How to use select_tag to create drop down menus in Rails

<%=
select_tag :size, options_for_select(["Small", "Medium", "Large"])
%>

This will create a drop down with the values in the array. You can either declare a constant array in the model and use that like: MyModel::SHIRT_SIZES instead of hard coding it. If it is database driven, write a helper that will return an array of options.

Sunday, July 01, 2007

How to find the helper methods for RESTful nested routes in Rails


1) script/console
2) >> irb ActionController::Routing::Routes
3) rs = ActionController::Routing::Routes
4) rs.named_routes.each {|name, route| puts( name, route) }; 1
5) Just copy the list of helper methods show in the output of step 4 and append path to that name. You must also provide the required ids shown in the output. For example take the output:

user
GET /users/:id/ {:action=>"show", :controller=>"users"}

and in the console, type:

>> user_path 1
=> "/users/1"

The same process can be used for nested routes.

Reference: Rails Routing by David A Black. This book gave me a solid understanding of routes.