首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >rails深度嵌套的form_for不能保存两次

rails深度嵌套的form_for不能保存两次
EN

Stack Overflow用户
提问于 2017-03-10 23:08:26
回答 1查看 78关注 0票数 0

我有一个表格,球队可以注册一个锦标赛。当我第一次保存表单时,表单会按预期保存& routes,但是如果我返回到root_path并重复注册团队的过程,表单根本不会保存(单击"Submit“按钮什么也不做)。

我的感觉是,原来的保存中有一些东西挂在那里,阻止了一个新的保存发生,但我不能确定它是什么。代码如下,希望它是足够的。

(routes.rb)

代码语言:javascript
代码运行次数:0
运行
复制
root 'tournaments#index'
  resources :tournaments do
    resources :teams do
      resources :players, :only => [:new, :create]
    end
  end

(型号)

代码语言:javascript
代码运行次数:0
运行
复制
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)

代码语言:javascript
代码运行次数:0
运行
复制
  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)

代码语言:javascript
代码运行次数:0
运行
复制
    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

从tournaments#show页面到teams#new操作的入口链接(此操作有效...只要它链接到正确的页面)

代码语言:javascript
代码运行次数:0
运行
复制
<%= link_to(new_tournament_team_path(params[:id])) do %><div class="rounded_btn"><h3>Register Team</h3></div><% end %>

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

代码语言:javascript
代码运行次数:0
运行
复制
<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输出:

代码语言:javascript
代码运行次数:0
运行
复制
<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时,此问题仍然存在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-11 00:15:22

我从我的/views/application.erb.html文件中删除了<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> &现在函数似乎可以工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42721600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档