Controlling parameters passed in rubyamf (rails + flex)
How do I control what parameters are passed in a RemoteObject method? I noticed that when I directly download and run the code from this site, saving an object results in the following parameter set being passed back:
Processing PostsController#save (for 127.0.0.1 at 2011-02-01 23:34:55) [POST]
Parameters: {0=>{"post"=>#<Post id: nil, title: "a", body: "b", created_at: nil, updated_at: nil>}, "post"=>#<Post id: nil, title: "a", body: "b", created_at: nil, updated_at: nil>}
whereas my own project (which basically involved following the same steps as in that tutorial) makes saves that give traces like
Processing CarsController#sa开发者_运维技巧ve (for 127.0.0.1 at 2011-02-01 22:34:56) [POST]
Parameters: {0=>{"car"=>#<Car id: nil, user_id: 0, name: "asdfCar", body_id: 3, theme: nil, deleted: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, created_at: nil, updated_at: nil>}}
To clarify, the difference is that the requests to the posts controller seem to receive two copies of the post, one as params[0]["post"] and one as params["post"], whereas my code only gives one.
The only code that seems to define the RemoteObject call is
<mx:RemoteObject id="postRO" destination="rubyamf" endpoint="rubyamf/gateway" source="PostsController" showBusyCursor="true" fault="onFault(event)">
<mx:method name="index" result="onIndexResult(event)"/>
<mx:method name="save" result="onSaveResult(event)"/>
<mx:method name="destroy" result="onDestroyResult(event)"/>
</mx:RemoteObject>
private function onAddPost(event:MouseEvent):void
{
var post:PostVO = new PostVO();
post.title = addTitleText.text;
post.body = addBodyText.text;
post.comments = new Array();
postRO.getOperation("save").send({post:post});
}
and then the value object definition is
package com.unitedmindset.vo
{
[RemoteClass(alias="PostVO")]
[Bindable]
public class PostVO
{
public function PostVO()
{
}
public var id:int;
public var title:String;
public var body:String;
public var createdAt:Date;
public var updatedAt:Date;
public var comments:Array;
}
}
while my own code looks damn similar with
private function onAddCar(event:MouseEvent):void
{
var car:CarVO = new CarVO();
car.name = addNameText.text;
car.bodyId = int(addBodyIdText.text);
car.componentInstances = new Array();
carRO.getOperation("save").send({car:car});
}
<mx:RemoteObject id="carRO" destination="rubyamf" endpoint="http://localhost:3000/rubyamf/gateway" source="CarsController" showBusyCursor="true" fault="onFault(event)">
<mx:method name="index" result="onIndexResult(event)"/>
<mx:method name="save" result="onSaveResult(event)"/>
<mx:method name="destroy" result="onDestroyResult(event)"/>
</mx:RemoteObject>
<mx:RemoteObject id="componentInstanceRO" destination="rubyamf" endpoint="http://localhost:3000/rubyamf/gateway" source="ComponentInstancesController" showBusyCursor="true" fault="onFault(event)">
<mx:method name="save" result="onCreateComponentInstanceResult(event)"/>
</mx:RemoteObject>
package com.foo.vo
{
[RemoteClass(alias="CarVO")]
[Bindable]
public class CarVO
{
public function CarVO()
{
}
public var id:int;
public var userId:int;
public var name:String;
public var bodyId:int;
public var createdAt:Date;
public var updatedAt:Date;
public var componentInstances:Array;
}
}
I assume there is some sort of configuration setting (presumably in Flex), but I can't figure out what it is. Any suggestions on where to look? Thanks.
Solved my problem. In rubyamf_config.rb
(in the config folder), needed to add the line:
ParameterMappings.scaffolding = true
So, yeah, the setting was in rails rather than in flex. I guess rails starts up, then when flash starts up it asks rails what format it wants responses in (which makes sense because it also has to deal with the ClassMappings), then flash posts to the rails app using the agreed-upon format.
精彩评论