Skip to content

Instantly share code, notes, and snippets.

@djtal
Created November 11, 2010 11:53
Show Gist options
  • Select an option

  • Save djtal/672387 to your computer and use it in GitHub Desktop.

Select an option

Save djtal/672387 to your computer and use it in GitHub Desktop.
<li class="party">
<% f.fields_for party, :index => index do |p| %>
<%= text_field_tag "party[game][#{Time.now.to_i}]", nil, :class => "party_game_name", :size => 40 %><br />
<div class="auto_complete"></div>
<%= p.hidden_field :game_id, :class => "party_game_id" %>
<%= p.hidden_field :created_at %>
<% p.simple_fields_for :sub_parties do |sub| %>
<%= sub.association :game, :collection => @extensions %>
<%= sub.hidden_field :created_at %>
<% end %>
<% end %>
</li>
<% remote_form_for "parties", :html => {:class => "party-form", :id => "parties-form-#{@date.to_s}"} do |f| %>
<strong>Jeux jou&eacute;(s)</strong>
<ul id="parties">
<% @parties.each_with_index do |party, index| %>
<%= render :partial => "form", :locals => {:f => f, :party => party, :index => index} %>
<% end %>
</ul>
<%= link_to_remote("Plus de parties...", {:url => add_party_form_parties_path(:date => @parties.first.created_at.to_date), :method => :get},
:class => "ss_sprite ss_add left") %>
<div class=" clear">
<div class="right">
<strong><span class="link reset">Annuler</span> ou <%= submit_tag "Enregistrer" %></strong>
</div>
</div>
<% end %>
I would like to register multiple parties at once and each party can have multiple sub_parties.
With code above sub_parties are not created since they dont belogn to the correct hash when submitted to controller
here what i get in the controler params hash
"parties"=>{"party"=>{"0"=>{"created_at"=>"2010-11-11 00:00:00 +0100", "game_id"=>""}, "1"=>{"created_at"=>"2010-11-11 00:00:00 +0100", "game_id"=>""}, "2"=>{"created_at"=>"2010-11-11 00:00:00 +0100", "game_id"=>""}, "sub_parties_attributes"=>{"0"=>{"created_at"=>"2010-11-11 00:00:00 +0100", "game_id"=>""}, "1"=>{"created_at"=>"2010-11-11 00:00:00 +0100", "game_id"=>""}}}}, "controller"=>"parties", "party"=>{"game"=>{"1289474165"=>""}}
the parties part is ok but sub_parties should be in each parties.
The app is a rails 2.3 apps not 3
If somebody can helps me with that
def new
@date = params[:date].to_date
@extensions = Game.extensions.find(:all)
@parties = []
3.times do
p = current_account.parties.build(:created_at => @date)
2.times do
p.sub_parties.build(:created_at => @date)
end
@parties << p
end
respond_to do |format|
format.js
end
end
class Party < ActiveRecord::Base
validates_presence_of :game_id, :account_id
belongs_to :game
belongs_to :account
has_many :players, :dependent => :destroy
has_many :sub_parties, :dependent => :destroy, :class_name => "Party"
accepts_nested_attributes_for :sub_parties
after_create :up_partie_cache, :update_played_date
after_destroy :down_partie_cache
//parties methods here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment