I'm working on a Flex/Rails app. I've got a model with a has_many :through
association that I'm trying to create. I've got it working with checkboxes on the plain rails pages, with the help of blogs like Paul Barry's has-many-through-checkboxes. Now I'm trying to get Flex to do the same and am having difficulty on how to send the equivalent checkbox parms in the Flex service call.
The model involved looks like this:
class PlayerAction < ActiveRecord::Base
belongs_to :player
belongs_to :action_type
has_many :action_cards
has_many :deck_cards, :through => :action_cards
end
The form on the rails page is this:
<% form_for PlayerAction.new do |f| %>
<%= f.hidden_field :player_id, {:value => @player.id} %>
<%= f.collection_select(:action_type_id, ActionType.find(:all), :id, :desc) %>
<ul>
<% @player.hand.deck_cards.each do |deck_card| -%>
<li><%= check_box_tag "player_action[deck_card_ids][]", deck_card.id -%><%= deck_card.title %>
<% end -%>
</ul>
<%= f.submit 'Do Action' %>
<% end %>
This creates parameters which, in the rails log, looks like this:
Parameters: {"commit"=>"Do Action", "action"=>"create", "controller"=>"player_actions", "player_action"=>{"action_type_id"=>"1", "player_id"=>"9", "deck_card_ids"=>["87", "56"]}}
Using the TamperData plugin on Firefox, the parameters look like this:
player_action[player_id]=9
player_action[action_type_id]=1
player_action[deck_card_ids][]=87
player_action[deck_card_ids][]=56
commit=Do+Action
and finally, my Flex code for the service call is as follows. svcAction
is an mx:HTTPService
defined elsewhere. The routing and url works, I'm focusing on the params here :
svcAction.url = "/player_actions.xml";
var params:Object = new Object();
params['player_action[action_type_id]'] = 1;
params['player_action[player_id]'] = 8;
params['player_action[deck_cards_ids][]'] = 37;
params['player_action[deck_cards_ids][]'] = 19;
svcAction.send(params);
In the above code, the player_action[deck_cards_ids][]
parms overwrite each other, so only one is sent, with the 2nd value. I've also tried arrays of the ids with
params['player_action[deck_cards_ids]'] = myIdsArray.toString();
and
params['player_action[deck_cards_ids]'] = "[" + myIdsArray.toString() + "]";
but neither of those worked either.
Any suggestions?