Posts Tagged ‘Headers’

27
Apr

Flex on Rails is too fast

Ruby on Rails evolves so fast that at version 2.3.2 all the code from a year 2008 book doesn’t work in April 2009.

Well, Flex + Ruby on Rails is great, the speed with which simple applications are made is astonishing. I’m reading right now Flex on Rails: Building Rich Internet Applications with Adobe Flex 3 and Rails 2 and as I said every once and a while I get stuck into compatibility problem.

Right now the problem is the following — in Flex HTTPService class though it says that method = “GET|POST|PUT|DELETE”, but the actual data is sent with GET or POST (for some reason I’m sure that 90% users don’t even know that PUT and DELETE exist). But in case with RoR they are essential, for example GET accounts/1 returns account information, PUT accounts/1 updates account and DELETE accounts/1 deletes an account. The book says just that and as a workaround it suggests to add ?_method=put to URL. But in 2.3.2 it doesn’t work anymore. Trying to do that we get

ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.)

Google says that I should to send method=put with request itself. Requests are sent in XML but I can’t have 2 roots in AS3 XML, so <_method>put</_method><data /> doesn’t work. Wrapping the whole construct into another root fails.

After googling a bit more I found out that I must set header HTTP_X_HTTP_METHOD_OVERRIDE = PUT. But it didn’t work. I almost gave up but visited this chinese link where our communist friend solved the problem!. It seems that Rails somehow adds to headers another HTTP_ prefix and doesn’t know what to do with things like HTTP_HTTP_X_HTTP_METHOD_OVERRIDE = PUT.

So, here’s the code which works

<mx:HTTPService id=”accountsUpdate”
url=”{CONTEXT_URL}/accounts/{accountsGrid.selectedItem.id}”
method=”POST” resultFormat=”e4x”  contentType=”application/xml” headers=”{{X_HTTP_METHOD_OVERRIDE: ‘PUT’}}” >
</mx:HTTPService>

I hope that this text helps people trying to learn Ruby on Rails and how it works with Flex.

Tags: , , , , , , , ,