Tagged with Rails

Rails Composer – Easily setup a new rails project

I can’t even count the number of rails projects I’ve created over the last five years. For a while I’ve been using the site RailsWizard to take away some of the drudgery of setting up the typical gems I like to work with. Recently someone introduced me to an alternative called Rails Composer.

If you end up on their website it can be difficult to figure out that the functionality they offer is actually free. You can find it, however, if you go to the projects repo page on GitHub. The money line is this:

$ rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb

I suggest you give it a shot. So far it looks really promising to me.

Tagged , , , ,

Manually generating #yeg trash data

Unfortunately Alertzy is running out of the garbage pickup data that was published by the City of Edmonton on their open data catalogue.  I’ve been assured that the data will be up shortly, but our data was set to run out on May 31st and that was starting to get a little too close for my comfort.

 

I wrote a quick rake task (Alertzy runs on Rails) to generate the data for June 2011 while we wait for the data catalogue to get up and running.  I encourage anyone to borrow the code (and point out any mistakes they find!) if they are in a similar position.  The code can be found here:
namespace :alertzy do
  desc "Populate June 2011 data"
  task :populate_june => :environment do
    generate_june_data
  end
end
 
def generate_june_data
  mondays = [6,13,20,27]
  tuesdays = [7,14,21,28]
  wednesdays = [1,8,15,22,29]
  thursdays = [2,9,16,23,30]
  fridays = [3,10,17,24]
 
  mondays.map! {|day| DateTime.parse("June #{day}, 2011 7:00:00")}
  tuesdays.map! {|day| DateTime.parse("June #{day}, 2011 7:00:00")}
  wednesdays.map! {|day| DateTime.parse("June #{day}, 2011 7:00:00")}
  thursdays.map! {|day| DateTime.parse("June #{day}, 2011 7:00:00")}
  fridays.map! {|day| DateTime.parse("June #{day}, 2011 7:00:00")}
 
  mondays.each do |date|
    [{:zone => "D", :day => 5},
     {:zone => "D", :day => 6},
     {:zone => "D", :day => 7}].each do |zone|
       GarbagePickup.create(:zone => zone[:zone], :day => zone[:day], :pickup_date => date)
    end
  end
  tuesdays.each do |date|
    [{:zone => "E", :day => 7},
     {:zone => "E", :day => 8}].each do |zone|
       GarbagePickup.create(:zone => zone[:zone], :day => zone[:day], :pickup_date => date)
    end
  end
  wednesdays.each do |date|
    [{:zone => "A", :day => 1},
     {:zone => "A", :day => 2}].each do |zone|
       GarbagePickup.create(:zone => zone[:zone], :day => zone[:day], :pickup_date => date)
    end
  end
  thursdays.each do |date|
    [{:zone => "B", :day => 2},
     {:zone => "B", :day => 3},
     {:zone => "B", :day => 4}].each do |zone|
       GarbagePickup.create(:zone => zone[:zone], :day => zone[:day], :pickup_date => date)
    end
  end
  fridays.each do |date|
    [{:zone => "C", :day => 4},
     {:zone => "C", :day => 5}].each do |zone|
       GarbagePickup.create(:zone => zone[:zone], :day => zone[:day], :pickup_date => date)
    end
  end
end
Tagged , , , , ,

Ruby on Rails – Nested Object Forms using a :belongs_to association

In a project I am currently working on, I have a one-way relationship using a belongs_to association.  I wanted to use the newer Nested Object Forms functionality added in Rails 2.3 to allow me to modify the properties of the related object in one form.

For the purposes of this post I’m going to use the following simplified code:

#address.rb
class Address < ActiveRecord::Base
  attr_accesible :address_string
end

#user.rb
class User < ActiveRecord::Base
  attr_accessible :name

  belongs_to :address
  accepts_nested_attributes_for :address
end

I ran into an issue when I was testing out this functionality in script/console and trying to assign attributes to the address through the user. I would encounter the error “WARNING: Can’t mass-assign these protected attributes: address_attributes” in my development.log. I proceeded to try and google a solution to this problem, and found many posts from users which encountered this issue, but none of them provided a solution. It appeared many people simply abandoned attempting to use nested attributes with a :belongs_to association. The solution to this problem is actually quite simple (once you know it!). Here’s the console log and the database log from my initial attempt:

#script/console
>> user = User.first=> #<User id: 1, name: "Bill", address_id: 1, created_at: "2010-08-20 21:49:52", updated_at: "2010-08-20 22:06:37">
>> params = {:address_attributes => {:address_string => "Up the tree", :id => 1}}
=> {:address_attributes=>{:address_string=>"Up the tree", :id=>1}}
>> user.update_attributes(params)=> true
>> user.address=> #<Address id: 1, address_string: "down the street", created_at: "2010-08-20 21:49:53", updated_at: "2010-08-20 21:49:53">


#development.log     
WARNING: Can't mass-assign these protected attributes: address_attributes

Note that the console even returns true from the operation, but the update address attributes aren’t assigned and the string is still “down the street” instead of “Up the tree” that we were trying to assign. The solution to this problem is to make sure that address attributes are added to the attr_accesible in the user model:

#user.rb
class User < ActiveRecord::Base
  attr_accessible :name, :address_attributes
  belongs_to :address
  accepts_nested_attributes_for :address
end

Now the error will no longer occur, and the address will be succesfully updated, as in the following console log.

#script/console
>> user = User.first=> #<User id: 1, name: "Bill", address_id: 1, created_at: "2010-08-20 21:49:52", updated_at: "2010-08-20 22:06:37">
>> params = {:address_attributes => {:address_string => "Up the tree", :id => 1}}
=> {:address_attributes=>{:address_string=>"Up the tree", :id=>1}}
>> user.update_attributes(params)                                               => true
>> user.address=> #<Address id: 1, address_string: "Up the tree", created_at: "2010-08-20 21:49:53", updated_at: "2010-08-20 22:41:46">

I hope this post can help save some time for others who run across this same issue.

Happy Coding!

Tagged , , , , , , ,

Ruby on Rails – Default Text Field Text That Clears On Click

I’m going to try using posterous’s code tags to make my first code post on this blog.  Bear with me if things don’t work out perfectly.

The designer on a project I’m working on (www.alertzy.com) wanted to have the effect where there is default text inside a text field, but it clears once the user clicks in the box.  The effect looks like this, before and after a user clicks on the text field (thanks to www.meshcanada.com for this example):

Beforeclick
Afterclick

As I am building this project in Ruby on Rails, I decided to write a simple helper to accomplish this behavior.  The helper itself is pretty straightforward, and I’ll leave dissecting the code as an exercise for you.  If you have any questions or suggestions for improvement feel free to post them in the comments.  Place this code in your application_helper.rb file in your Rails project.

  def default_text(text)
    onFocusFunction = "field = event.target || event.srcElement; if (field.value=='#{text}') {field.value = '';}else {return false}"
    onBlurFunction = "field = event.target || event.srcElement; if (field.value=='') {field.value = '#{text}';}else {return false}"
    {:value => text, :onfocus => onFocusFunction, :onblur => onBlurFunction}
  end

Then in your views you can simply call this helper function in the parameters of a field to add this behavior.  Here is an example for a text field for the users username which will display the text “Login” in the field.

<%= text_field_tag 'login', @login , default_text("Login")%>

The end effect of this will look like this:

Helperbeforeclick
Helperafterclick
Happy Coding!

Update August 20th/2010: It was brought to my attention that this helper wasn’t working in Internet Explorer.  This is because IE uses event.srcElement instead of event.target.  The code in this post has been updated to reflect this and should now work cross-browser.

Update August 28th/2010: I had forgotten that this functionality has been added to text fields in HTML 5 under the attribute “Placeholder”.  This technique is still useful for getting this functionality in non-HTML 5 compliant browsers.

Tagged , , , , ,