rails深层嵌套form_for无法保存两次

时间:2017-03-10 15:08:26

标签: ruby-on-rails forms ruby-on-rails-5 form-for nested-form-for

我有一个团队可以注册比赛的表格。表单保存&我第一次保存表单时按预期路由,但是如果我返回root_path&重复注册团队的过程,表单根本不保存(点击“提交”按钮什么都不做)。

我的感觉是,原始豁免中出现了一些阻止新事物发生的事情,但是我无法指出它是什么。代码如下,希望足够。

(routes.rb中)

root 'tournaments#index'
  resources :tournaments do
    resources :teams do
      resources :players, :only => [:new, :create]
    end
  end

(型号)

class Tournament < ApplicationRecord
  has_many :teams
  has_many :players, :through => :teams

  accepts_nested_attributes_for :teams  
  accepts_nested_attributes_for :players  
end

class Team < ApplicationRecord
  belongs_to :tournament, required: false
  has_many :players

  accepts_nested_attributes_for :players
end

class Player < ApplicationRecord
  belongs_to :team, required: false
  has_one :tournament, :through => :team
end

(tournaments_controller.rb)

  def index
    @tournaments = Tournament.all
  end

  def show
    @tournament = Tournament.find(params[:id])
  end

  def new
    @tournament = Tournament.new
  end

  def create
      @tournament = Tournament.new(tournament_params)
      if @tournament.save
      flash[:notice] = "#{@tournament.name} saved."
      redirect_to root_path
    else
      render :new
    end
  end


  private

  def tournament_params
    params.require(:tournament).permit(:name, :deadline, :divisions, :info, :payment, :tournament_date, team_attributes: [:name, :division, :contact_email, :contact_name])
  end

(teams_controller.rb)

    def create
        @team = Team.new(team_params)
        if @team.save
          flash[:notice] = "Team Registered."
          redirect_to tournament_team_path(params[:tournament_id], @team.id)
        else
          redirect_to new_tournament_team_path(params[:tournament_id])
        end     
    end

    def new
        @tournament = Tournament.find_by_id(params[:tournament_id])
        @team = Team.new
        8.times do
          @team.players.build
        end
    end

    def show
    @tournament = Tournament.find(params[:tournament_id])
    @team = Team.find(params[:id])
    end


    private

  def team_params
    params.require(:team).permit(:name, :tournament_id, :division, :contact_name, :contact_email, players_attributes: [ :name, :gender, :referee ])
  end

锦标赛的参赛链接#显示页面到团队#news action(这是有用的......只要它链接到正确的页面)

<%= link_to(new_tournament_team_path(params[:id])) do %><div class="rounded_btn"><h3>Register Team</h3></div><% end %>

(teams / new.html.erb) - 这是不起作用的表单。我应该使用偏见,但我想让它开始工作。

<main class="long_page">
    <h1 class="tournament_name"><%= @tournament.name %></h1>
    <h3 class="tournament_name">Team Registration</h3>
<section style="align-items: baseline;">
    <div class="rounded_box">
        <%= form_for [@tournament, @team] do |f| %>
        <%= f.hidden_field :tournament_id, :value => @tournament.id %>
            <div>
             <p>
                <%= f.label :division, "Division: " %>
                <%= f.select :division, options_for_select([["Mixed", "mixed"], ["Mens", "mens"]]) %>
            </p>
            <p>
                <%= f.text_field :name, required: '' %>
                <label alt='Team Name' placeholder='Team Name'></label>
            </p>
            <p>
                <%= f.text_field :contact_name, required: '' %>
                <label alt='Contact Person' placeholder='Contact Person'></label>
            </p>
            <p>
                <%= f.text_field :contact_email, required: '' %>
                <label alt='Contact Email' placeholder='Contact Email'></label>
            </p>
            <div class="clear"></div>
            </div>

            <%= f.fields_for :players do |builder| %>
                <div>
                   <%= builder.text_field :name, required: '' %>
                   <%= builder.select :gender, options_for_select([["Male", "male"], ["Female", "female"]]) %>
                   <%= builder.select :referee, options_for_select([["No", "no"], ["Yes", "yes"]]) %>

                </div>
            <% end %>

            </div>
            <div class="btn_holder">
            <p>
    <%= f.submit 'Submit Team', :class => 'rounded_btn' %>
</p></div>
        <% end %>
    </div>
</section>

这将生成以下HTML输出:

<body>
    <container>
      <header class="header" id="header">
    <a href="/"><img alt="Taipei Touch Association Logo" src="/assets/taipei_touch_logo_faded-4bbdd185e462f4d8af3b2d25f221f27f4ae479c8adfa4701b8c3f03e9e31b36c.svg"></a>
    <span class="header_span">
        <h4>Taipei Touch Association</h4>
        <h3>Tournament Management System</h3>
    </span>
</header>
    <main class="long_page">
    <h1 class="tournament_name">Dummy Tournament</h1>
    <h3 class="tournament_name">Team Registration</h3>
<section style="align-items: baseline;">
    <div class="rounded_box">
        <form class="new_team" id="new_team" action="/tournaments/1/teams" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="+CtsL2QcMjj1703iGPpwgs8vYu7TWJfWt9jTUgCYTMaFE9JdvYIMJqdb87z4vKuDzUgb8RO96Mdvk5fW/uJUSw==">
        <input value="1" type="hidden" name="team[tournament_id]" id="team_tournament_id">
            <div>
             <p>
                <label for="team_division">Division: </label>
                <select name="team[division]" id="team_division"><option value="mixed">Mixed</option>
<option value="mens">Mens</option></select>
            </p>
            <p>
                <input required="required" type="text" name="team[name]" id="team_name">
                <label alt="Team Name" placeholder="Team Name"></label>
            </p>
            <p>
                <input required="required" type="text" name="team[contact_name]" id="team_contact_name">
                <label alt="Contact Person" placeholder="Contact Person"></label>
            </p>
            <p>
                <input required="required" type="text" name="team[contact_email]" id="team_contact_email">
                <label alt="Contact Email" placeholder="Contact Email"></label>
            </p>
            <div class="clear"></div>
            </div>


                <div>
                   <input required="required" type="text" name="team[players_attributes][0][name]" id="team_players_attributes_0_name">
                   <select name="team[players_attributes][0][gender]" id="team_players_attributes_0_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][0][referee]" id="team_players_attributes_0_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][1][name]" id="team_players_attributes_1_name">
                   <select name="team[players_attributes][1][gender]" id="team_players_attributes_1_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][1][referee]" id="team_players_attributes_1_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][2][name]" id="team_players_attributes_2_name">
                   <select name="team[players_attributes][2][gender]" id="team_players_attributes_2_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][2][referee]" id="team_players_attributes_2_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][3][name]" id="team_players_attributes_3_name">
                   <select name="team[players_attributes][3][gender]" id="team_players_attributes_3_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][3][referee]" id="team_players_attributes_3_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][4][name]" id="team_players_attributes_4_name">
                   <select name="team[players_attributes][4][gender]" id="team_players_attributes_4_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][4][referee]" id="team_players_attributes_4_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][5][name]" id="team_players_attributes_5_name">
                   <select name="team[players_attributes][5][gender]" id="team_players_attributes_5_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][5][referee]" id="team_players_attributes_5_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][6][name]" id="team_players_attributes_6_name">
                   <select name="team[players_attributes][6][gender]" id="team_players_attributes_6_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][6][referee]" id="team_players_attributes_6_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

                <div>
                   <input required="required" type="text" name="team[players_attributes][7][name]" id="team_players_attributes_7_name">
                   <select name="team[players_attributes][7][gender]" id="team_players_attributes_7_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
                   <select name="team[players_attributes][7][referee]" id="team_players_attributes_7_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>

                </div>

            </form></div>
            <div class="btn_holder">
            <p>
    <input type="submit" name="commit" value="Submit Team" class="rounded_btn" data-disable-with="Submit Team">
</p></div>

</section>
    </main>
    </container>




</body>

注意:即使使用nested_forms gem,此问题仍然存在。

1 个答案:

答案 0 :(得分:0)

我从/views/application.erb.html文件&amp;中删除了<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>现在功能似乎有效。