Oct 9

Generate a Rails version specific project

Category: Development, Rails

If you have rails 2.x installed but need to generate a rails 1.x project, you can use the version tag as follows:

rails _1.2.6_ project

Just replace the 1.2.6 with whatever version you need, and make sure you use underscores before and after the version.

No comments

Sep 15

Ruby sum array values for currency

Category: Uncategorized

I found this handy trick to take an array built from some hash values and spit out a currency. Here is what I used it for. Inside my model, I built some test items.

def self.test_items
    [{:price => "5.50", :weight => 10, :item_description => "Test item 1"}, {:price => "7.75", :weight => 15, :item_description => "Test item 2"}, {:price => "9.95", :weight => 20, :item_description => "Test item 3"}]
end

I wanted to get a “Total Price” in the view, so I did the following. I created a method in my ApplicationHelper that looks like so

def total_for_cart(prices)
    number_to_currency(prices.inject(0){|sum, price| sum.to_f + price.to_f})
end

The above uses the Ruby inject method from the Enumerable class to simply add the float values. I’m also using the rails helper method number_to_currency for pretty formatting. Finally my view looks like this:

<p>Total: <%= total_for_cart(items.collect{|i| i[:price]}) %></p>

That simply collects the price from the items array and passes it to my helper method. Hope its helpful!

No comments

Sep 10

Ruby IRB Rails script/console tricks

Category: Development, Rails

I use script/console to play with any new features that I’m doing before they even make it into my app. I love using irb and script/console, but there were a few things that frustrated me.
1. Exit and reload the console when I make model changes
2. Command history was blown away on exit

So I decided to take some time to see if I could come up with solutions for the above. Luckily I found solutions for both of my problems.
Issue #1. After you make changes simply type “reload!” at the prompt to reload the app. This solved most of my problems, but still if I exit I lose all my previous commands.

Issue #2. Create the file ~/.irbrc to configure your irb settings. Include the following settings:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"

Voila, you now have a saved history just like a bash console. Use the “Up” and “Down” keys to browse your old history.

No comments

Sep 8

Ubuntu SVN cannot set LC_CTYPE locale

Category: Linux, Ubuntu

I ran into this error on a fresh Ubuntu Hardy server I set up hosting a SVN repository.  The error messages may look like the following:

 
svn: warning: cannot set LC_CTYPE locale
svn: warning: environment variable LANG is en_US.UTF-8
svn: warning: please check that your locale name is correct

This won’t cause any problems with the functionality of Subversion, but it gets annoying.  So the problem is that the LANG that is specified (in my case en_US.UTF-8) isn’t installed.  So, to install it do the following:

 
apt-get install language-pack-en-base

This will ask you if you want to install language-pack-en also, so say yes to the prompt.  Then you will be set.  If you language is something other than English “en”, then install the appropriate locale package.

No comments

Aug 6

Retrieve more than 200 records from SalesForce using ActiveSalesForce gem

Category: Development, Rails

When you do a find on an ActiveSalesForce model, there is a default limit of 200 records. To override that value, pass “:limit => 0″ in your find. Here is an example

Contact.find(:all, :limit => 0)

Hope this saves some frustrations.

No comments

Jul 16

Rails TimeZone support

Category: Development, Rails

I’m trying to use the built in TimeZone class to adjust for timezones, but it doesn’t appear to be working properly. Here is what I’m seeing in script console in Rails 2.0.2. Saw similar results in 2.1.0

>> time_zone = TimeZone.all.select{|tz| tz.name == "Mountain Time (US & Canada)"}.pop
>> time_zone.adjust(Time.now)
=> Wed Jul 16 08:22:34 -0600 2008
>> Time.now
=> Wed Jul 16 09:22:44 -0600 2008

The above is the same as running time_zone.now. Every timezone I tried appears to be an hour off. The tzinfo plugin on the other hand appears to be working properly.

>> tz = TZInfo::Timezone.get('America/Boise')
>> tz.now
=> Wed Jul 16 09:17:51 UTC 2008 (which was the correct time when I ran it)

Is this a bug or am I not using it properly. The docs are pretty skimpy for this class.

No comments

Jul 14

Make your rails form go more places (multiple actions)

Category: Development, Rails

Sometimes you may have the need to submit a form to different actions. This came in handy for a form we had to integrate with an external app.  So here is one way to do it.

In your view, do your normal form_for tag with a “default” action This is the action that will be use via the submit button. Then add two links that call customize_form_action passing in a value used to differentiate the destination. You could even just pass in the action name if you wanted. I had to do it a little different because I was using different controllers also.

<%- form_for @foo, :url => foo_path, :html => {:id => 'foo_form'} do |f| -%>
  <%= render :partial => 'form_fields' %>
  <%= link_to 'Another Action', "#", :onclick => "customize_form_action('action1');" %> 
  <%= link_to 'Yet Another', "#", :onclick => "customize_form_action('action2');" %>
<%- end -%>

Now in your script tag or JS file, do the following:

function customize_form_action(action){
	var action_form = $('foo_form');
	switch(action){
		case 'action1':
			action_form.action = '/controller/new1';
			break;
		case 'action2':
			action_form.action = '/controller/new2';
	}
	action_form.submit();
}

One other option would be to use multiple submit buttons with a unique value to differentiate them. Then in JS observe the form ’submit’ and handle the action there.

No comments

Jul 2

Ruby on Rails InvalidAuthenticityToken on Ajax Request

Category: Rails

This one has really been bothering me. I believe it was in Rails 2.0.2 they added some forgery protection methods. This makes it a bit more difficult to do ajax posts. The first workaround I found was just to add the following line into the controller

protect_from_forgery :except => :your_ajax_update

However, I felt this was more of a hack than a fix. So I’ve finally found a fix that works for me. I am using an ajax request that is fired off using the :onchange attribute for a radio button. Here is what you need in your view.

<%= radio_button "object", "method", "value", :onchange => "new Ajax.Request('/controller/your_ajax_update',{asynchronous:true, evalScripts:true, parameters:{param1: '#{@blah.id}', param2: '#{bleh.id}', authenticity_token: encodeURIComponent('#{form_authenticity_token}')}})" %>
1 comment

Jul 2

Greybox Plugin for Rails

Category: Rails

This is my first post for my blog. It is also my first rails plugin I’ve done. I just wanted to learn more about the plugin architecture, so this is where I started.

I’ve been using Orangoo’s greybox on most of my sites I do lately. So I figured why not make a plugin for it so I don’t have to do the same setup over and over. Then I can share it with the world and make it a better place…. Well at least add some nice eye-candy to your site in one command. All the real credit goes to Orangoo for creating Greybox. Please see their Documentation for usage. For now I’ve only put it into the rails plugin architecture and wrapped the includes into a helper. There will be more to come soon though. OK, to install do the following:

Install

Inside your rails app type:
Linux and Mac

$ ./script/plugin install http://greybox-plugin.googlecode.com/svn/trunk/greybox

Windows

$ ruby script/plugin install http://greybox-plugin.googlecode.com/svn/trunk/greybox

Setup

Inside your layout include this between your head tags:

<%= greybox_includes %>

Thats it. Refer to the documentation for usage. Here are some basics:

Usage

inside your views, here is how you use it:
Link to another action “full screen”

<%= link_to "Details", {:controller => :hours, :action => :details, :id => project.id}, :rel => 'gb_page_fs[]' %>

Here is a screenshot of an app using greybox with a fullscreen page loaded.

Link to a group of images
link_to name, image_source, html_options

html_options:
rel is the type of gb link to build
title is used as the caption in the gb window

<%= link_to( 'Cool Link', "images/balloon/img07.jpg", :rel => "gb_imageset[pics]", :title => "Image 1") %>
<%= link_to( 'Another Cool Link', "images/balloon/img06.jpg", :rel => "gb_imageset[pics]", :title => "Image 2") %>

Here is a screenshot of the images group in greybox

No comments

Jun 18

Welcome

Category: Uncategorized

Hello all,

I decided to move my old blog to a wordpress instance.  Mephisto just wasn’t working out for me, regardless of my love for rails.  Anyhow, I’ll slowly be moving stuff over from my old blog.  Please stay tuned.

Thanks,

Shep

No comments