Jul 16
Rails TimeZone support
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 commentsJul 14
Make your rails form go more places (multiple actions)
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 commentsJul 2
Ruby on Rails InvalidAuthenticityToken on Ajax Request
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}’)}})” %>
Jul 2
Greybox Plugin for 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

Jun 18
Welcome
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