Thursday, October 15, 2009

can't convert Fixnum into String to_param

I was getting this error in to_param method. I had to explicity call to_s on id to fix it. My to_param method :

def to_param
name_slug ? "#{id.to_s}-#{name_slug.parameterize}" : id.to_s
end

If the name slug is not nil then it uses that to generate the SEO friendly URL otherwise it uses the id.

Saturday, September 19, 2009

Thursday, September 10, 2009

Hivelogic instructions for ruby path

If which ruby shows /usr/bin/ruby instead of /usr/local/bin/ruby , it means you have not set the PATH correctly. I had to add a line: source ~/.profile to the ~/.bash_profile file. Now whenever you bring up the terminal and when you check the PATH, usr/local/bin will come before /usr/bin and the latest installed version will be picked up (which ruby will output /usr/local/bin/ruby)

Thursday, August 13, 2009

Monday, July 20, 2009

How to dynamically insert html code with Javascript and Rails

somePage.html.erb

<%= text_field("myObjectName", "someAttribute", "onchange" => "calculateMyVar()") %>

When the user moves out of the text field, the javascript will be called.

<% if @myVar.nil? %>

<% else %>
<%= @myVar %>
<% end %>

Notice that, in order for this to work the value between the td tags is empty. If you have some dynamic output tag as shown in the else section then the javascript will not work. The above code will work for new and edit cases.

Include the following javascript within the head section of the html.erb file.

function calculateMyVar()
{
var f1 = document.getElementById('field1').value;
var f2 = document.getElementById('field2').value;
document.getElementById('myVar').innerHTML = f1 - f2
}

Rails Documentation Bug for Time extension

If you have a table with an attribute of type :time then to access the minutes, use min method. There is no minute or minutes method on Time extension object in ActiveSupport.

You can list all the methods available by: myActiveRecordObject.game_time.methods.sort

Friday, July 17, 2009

Thursday, July 16, 2009

How to replace space with comma in sed

test.txt :

Bugs Bunny
Daffy Duck

cat test.txt | sed -e 's/[ ]/,/g'

outputs:

Bugs, Bunny
Daffy, Duck

cat test.txt | sed -e 's/[ ]/,/g' | sed 's/$/,/'
will append comma to the end as well, so:

Bugs, Bunny,
Daffy, Duck,

Wednesday, March 04, 2009

How to load a specific version of gem

For some reason if you want to use specific version of gem installed on your system:

require 'rubygems'
gem 'activesupport', '=1.4.2'
require 'active_support'

Friday, February 27, 2009