:layout => false

Thursday 11/27/2008  –  Category: Merb  –  No Comments

I ran into a strange behavior when trying to use setInterval in JS to make an AJAX request every n seconds.  I had the timer in the $(document).ready(function() { } in the head of my application layout file.

There would be strange behavior with the timer--every interval would be twice as fast as the previous, and the browser would quickly become unresponsive and crash.

I initially thought it was a Firefox bug so I wasted sometime googling setInterval but it turns out the AJAX request was rendering the view with the layout, thus creating another timer on each request.  Stupid.

Setting :layout => false on the offending action made everything play nice.  Moral of the story: don't forget to exclude your layouts for AJAX actions.

Getting a random record using datamapper

Thursday 11/27/2008  –  Category: Uncategorized  –  1 Comment

Model.get(1+rand(Model.count))

You'll have to have dm-aggregates for Model.count to work correctly

ruby strip vs strip!

Thursday 11/27/2008  –  Category: Uncategorized  –  No Comments

Sorry to disappoint, but this entry isn't about strippers.

str.strip returns a copy of string str with with whitespace chopped off, while str.strip! returns the same string str or nil if the string wasn't altered.

I didn't realize str.strip! would return nil until I was getting a lot of empty strings back...

Merb rake tasks

Thursday 11/27/2008  –  Category: Merb  –  No Comments

When trying to run a merb rake task I wrote, I ran across this error:

rake aborted!
Don't know how to build task 'environment'

It turns out that merb uses a different environment variable than Rails,  so instead of :environment use :merb_env instead:

task :whatever => :merb_env do

merb and Twitter made easy with Twitter4R

Wednesday 11/26/2008  –  Category: Merb  –  No Comments

Updating twitter with merb is super easy with Twitter4R:

sudo gem install twitter4r

in config/dependencies.rb:

gem "twitter4r"
require 'twitter'

(just doing dependency "twitter4r" didn't work)

in your controller:

client = Twitter::Client.new(:login => 'username', :password => 'password')
status = client.status(:post, "hello world")

 Page 11 of 15  « First  ... « 9  10  11  12  13 » ...  Last »