Topic: assigning teams using a form...

I am trying to create a simple form where I can assign users who have registered to teams.

Basically I have a database where all the registrations for the season are stored and linked to user accounts via a user_id foreign key.

Conceptually I want to create a page that lists all the users registered for a particular league with check boxes next to each user. There would be a select list to choose the team color at the top of the page and a submit button at the bottom.

Passing over the value of the select box will be simple, but I am not sure how to handle the check boxes or the format they should come over to the method in... I have had trouble working with check boxes before and I am not sure how to handle this.

Any ideas or suggestions for a better way would be appreciated!

Re: assigning teams using a form...

Ok, I managed to figure out how to accomplish this - hopefully it helps someone should they need to do something similar...

First I create a page that list all the registrations for a particular league - simple enough. Then I create the following form...

#assign_player_teams
<% form_for :team_members, @users, :url => {:action => 'create_player_teams'} do |form| %>
    <table cellspacing = "0">
        <%= collection_select :user_teams, :team_id, Team.find(:all, :conditions => [ "league_id = ?", @league,]), :id, :team_name %>
        <%= hidden_field :user_teams, :session_id, :value => @session %>
           
        <% @registrations.each do |reg| %>
            <tr>
                <td><%= reg.user.user_info.last_name %>, <%= reg.user.user_info.first_name %></td>
                <td><%= form.check_box reg.user_id %></td>
            </tr>
        <% end %>
    </table>
    <p>
        <%= submit_tag "Assign Team" %>
    </p>
<% end %>

Basically this form displays a list box to choose the team from, puts the league id in a hidden field and display each registered player with a check box next to their name

Then in the controller I do the following:

#create_player_teams

def create_player_teams
    get_user_permissions(5)
    @team_members = params[:team_members].reject { | key, value | value != "1" }.keys.collect { | each | each.to_i }
    @teams = []
   
    @team_members.each do |team_member|
      member = UserTeam.new
      member.user_id = team_member
      member.aasa_session_id = params[:user_teams][:session_id]
      member.team_id = params[:user_teams][:team_id]
      @teams << member
    end
   
    @teams.each(&:save!)
      flash[:notice] = "User teams have been saved!"     
      redirect_to :action => 'list_player_team', :id => params[:user_teams][:team_id]
    rescue ActiveRecord::RecordNotSaved
      flash[:notice] = "There was a problem with the teams! Please resubmit."
      redirect_to :action => 'assign_player_teams'
end


The key is how the params get assigned to the @team_members variable. Basically I 'reject' item in the params hash that is not equal to 1 (ie not checked) and then convert the values to integers. After that I can loop through and create a new entry for each person (UserTeam.new), populate it with vaues and then eventually save.

Works great!

If anyone has a way to simplify this at all I would love get input.

Last edited by rburbach (2007-04-15 22:57:00)