Archive for the 'Rails' Category

CodeJam@Thoughtworks Beijing

Ye Zheng wrote quite a detailed account (in Chinese) about our first CodeJam in the Beijing office already, so I’ll just be brief. I had a great time pairing with other colleagues that I haven’t worked with before. In hindsight I was quite amazed how much we accomplished and how smooth it went.
I […]

Services != Distributed Object

(The title quotes Joshua Graham’s comment here) I agree completely with Josh. In a casual conversation with Tim Cochran of Thoughtworks, we both felt that using RESTful controllers does not mean exposing all of your model objects, or database tables. Thoughts still need to go into designing the right interfaces.
In Rails, examples of […]

ActiveResource and handling a weird 204 response from a remote rails controller

In the examples given in ActiveResource Rails API, when ActiveResource does a PUT (e.g. update) to a remote RESTful rails controller, the rails controller is supposed to return a HTTP code of 204 (No Content).
But if the remote rails controller actually does that:
head :no_content #204
The Http response that ActiveResource receives is a Net::HTTPNoContent, with no […]

Trying out ActiveResource in 3 minutes

Recently I started using ActiveResource to talk to a RESTful controller (which is also done in Rails 2). It is very nice and simplifies a lot. With Rails 2, out of the box you can create a sample RESTful server and client in 3 minutes:
Server:

>> rails test_server
>> cd test_server
>> script/generate scaffold Account […]

JSON is valid YAML

I was searching for simple JSON parsing in Rails and found only the Rails 2.0 way: ActiveSupport::JSON.decode. In old version of Rails (1.2.6) there’s only JSON.encode…
So I googled and found here that you can do YAML::load ‘{”key”:”value”}’

A few things I learned about ActiveRecord after_create (and other callbacks)

1. after_create does not get inherited
class Animal < ActiveRecord::Base
after_create :feed_on_milk
def feed_on_milk
self.immunity_level += 1
end
end
class Cat < Animal
end
The after_create does not get inherited, and so our poor little cats won’t get milk! To retain the after_create hook, you’ll need to do:
class Cat < Animal
after_create :feed_on_milk
end
2. after_create is an array of symbols
If you call after_create on same […]

Suppressing warnings in Ruby (in my case, assert_select)

assert_select outputs warnings to stderr about invalid HTML problems (something like a mismatching closing
), yet the assertion still passes/fails as expected. So, instead of fixing the real problem by digging through our layout and partials rhtmls and find the mismatching
, I just want to suppress the warnings. :)
From http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/210670
def […]