0

I have a form where user can create a player and also associate a team with the it. The player form has a select list where user can select an existing team or select the 'new' option which displays a popup which allows user to create a new team and associate it with the player.

I have used javascript to display the popup window when user selects 'new' option.

My new team form is as follows:

<% form_remote_tag :url => {:controller => 'players', :action => 'createTeam'} do %>
    <label style="width: 150px">Team name:</label> <%= text_field_tag(:team_name, nil) %>
    <%= submit_tag("Create New Team") %>
<% end %>

controller - players, action - createTeam

def createTeam
  @team = Team.create(:team_name => params[:team_name])

  respond_to do |format|
  if @team.save
    flash[:notice] = 'Team was successfully created'
    format.html { redirect_to teams_path }
    format.js
  else
    flash[:notice] = "Team failed to save."
    format.html { redirect_to teams_path }
  end

  end
end

I would like to be able to update the select list in my parent form (new.html.erb for player) dynamically each time i add a team.

I am a bit confused on how to do this.

I would be grateful if someone could give me an idea on how to go forward.

Many many thanks for any suggestion provided.

tanya
  • 2,925
  • 7
  • 32
  • 49

1 Answers1

0

You'll need to use something like replace_html to re-render the list. This SO question should help you out.

Community
  • 1
  • 1
Caley Woods
  • 4,707
  • 4
  • 29
  • 38