Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >rails深度嵌套的form_for不能保存两次

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

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

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

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

(routes.rb)

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

(型号)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
运行
AI代码解释
复制
  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
运行
AI代码解释
复制
    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
运行
AI代码解释
复制
<%= 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
运行
AI代码解释
复制
<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
运行
AI代码解释
复制
<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-10 16:15:22

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

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

https://stackoverflow.com/questions/42721600

复制
相关文章
MySQL括号字符串计数
现在想得出每种评论字数的个数,每个字符包括标点、空格、表情符号都算一个字,但每对中括号连同其中的内容只算一个字。对于上面的数据行,结果为:
用户1148526
2022/11/14
1.3K0
MySQL括号字符串计数
MySQL实现批量Insert和分页查询
DROP PROCEDURE IF EXISTS test_insert;--如果存在此存储过程则删掉
浩Coding
2019/07/03
4.1K0
MySQL实现批量Insert和分页查询
MySQL中INSERT INTO SELECT的应用
这种方法常用在目标表已经存在数据库中.需要从源表把数据插入到目标表的情况.如果目标表和源表的列相同,就不必列出数据列了.(未完待续,另参考资料中重复项太多,暂且作为记录,后期再做增删整理)
WindCoder
2018/09/20
3.3K0
MySQL 查询结果中处理字符串
但是如果查询过程中有一个字符串为 null 则整个结果都将是 null ,这时可以将 null 转换为 “
zucchiniy
2019/10/30
4.3K0
Mysql中的INSERT ... ON DUPLICATE KEY UPDATE
在日常业务开发中经常有这样一个场景,首先创建一条记录,然后插入到数据库;如果数据库已经存在同一主键的记录,则执行update操作,如果不存在,则执行insert操作;
美团骑手
2021/12/24
1.8K0
shell中的括号(小括号,中括号,大括号)
    ①命令组。括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用。括号中多个命令之间用分号隔开,最后一个命令可以没有分号,各命令和括号之间不必有空格。
雪影
2018/08/02
3.9K0
MySQL中insert阻塞问题的分析
今天收到一个业务报警,提示某个数据库实例的连接数暴涨,然后瞬间又恢复了,这种情况持续反复了几次,和开发同学沟通时,他们也希望能够得到更多的信息,比如是哪个数据库的连接数异常暴涨,我也想知道啊,但是苦于没有合适的工具和方法能够实现更细粒度的监控/统计,于是我着手开始分析这个问题。
jeanron100
2020/08/25
1.8K0
MySQL中insert阻塞问题的分析
前端传值不能带中括号???
将前端传值通过enencodeURI()进行转义,后台部分需要通过 URLDecoder.decode()进行解析
taixingyiji
2022/07/25
9940
前端传值不能带中括号???
Mysql中的关联查询(内连接,外连接,自连接)
在使用数据库查询语句时,单表的查询有时候不能满足项目的业务需求,在项目开发过程中,有很多需求都是要涉及到多表的连接查询,总结一下mysql中的多表关联查询
全栈程序员站长
2022/09/01
3.9K0
Mysql中的关联查询(内连接,外连接,自连接)
MySQL 中的 INSERT 是怎么加锁的?
来源:https://www.aneasystone.com/archives/2018/06/insert-locks-via-mysql-source-code.html
Java技术栈
2021/11/12
11.3K0
在字符串中查找id值MySQL
SELECT * FROM `hz_article_type` WHERE FIND_IN_SET( 5, items_id ) LIMIT 0 , 30
全栈程序员站长
2022/07/07
5.4K0
在字符串中查找id值MySQL
MySQL 多表查询、连接查询(内连接、外连接)
查询的结果是一个二维表,它是students表和classes表的“乘积”,即students表的每一行与classes表的每一行都两两拼在一起返回
Michael阿明
2020/07/13
3.8K0
MySQL 多表查询、连接查询(内连接、外连接)
Mysql中INSERT ... ON DUPLICATE KEY UPDATE的实践
在日常业务开发中经常有这样一个场景,首先创建一条记录,然后插入到数据库;如果数据库已经存在同一主键的记录,则执行update操作,如果不存在,则执行insert操作;
加多
2018/09/06
2.4K0
Mysql中INSERT ... ON DUPLICATE KEY UPDATE的实践
在 JavaScript 中优雅的提取循环内的数据 [每日前端夜话0x2D]
从 A 行开始的循环用来记录文件路径。它是 for-of 循环和递归的组合(递归调用在 B 行)。
疯狂的技术宅
2019/03/27
3.7K0
在 JavaScript 中优雅的提取循环内的数据 [每日前端夜话0x2D]
python从字符串中提取数字
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124581.html原文链接:https://javaforall.cn
全栈程序员站长
2022/07/21
3.3K0
SQL 从字符串中提取数字
mix 表有一个 varchar 类型的字段 v,该字段的允许长度只有 15 位,但它存储的数据比较混杂。
白日梦想家
2020/12/14
2.5K0
js提取正则中的字符串
代码如下: var results = data.match(/(start=').*?(')/); if (results != null) { console.log(data[0]);
磊哥
2018/05/08
5.5K0
Jmeter 正则表达式提取括号中的文本内容
     jmeter里接口请求结束后,如果后续接口请求想要获取本次返回结果的内容,就需要正则表达式提取器来获取参数,当然也可以用json path extractor来提取(这个简单一些)。不过Jmeter里很多地方的约束条件和断言都是需要正则表达式来匹配的,所以大家还是要学习一下正则表达式的语法,以便更好更效率的完成测试内容。下面简单介绍一下常用的语法,这次先写一半,下次再补另一半。正则表达式的内容还是很多的,我也只是学了其中的一些皮毛,通用的东西,后期再慢慢深入吧,而且要学以致用,学的东西一定要亲自实际体验一下才会记得牢,好了,言归正转。
全栈测试开发日记
2023/02/02
1.6K0
Jmeter 正则表达式提取括号中的文本内容
MySQL数据插入INSERT INTO与条件查询WHERE的基本用法(二)
上节课给大家介绍了数据库的基本概念以及如何创建数据库,具体可回顾MySQL创建数据库(一)。从本节课开始,我们将对MySQL中的基本知识点进行分别介绍。本节课先向大家介绍MySQL数据插入insert into与where条件查询的基本用法。
用户7569543
2021/10/20
4.5K0
点击加载更多

相似问题

从括号内提取(子)字符串

22

MySQL中的按组查询忽略括号内的值

10

从SQL insert查询字符串中提取引用的值

21

从python中括号内提取数据

13

如何从‘`onclick`’属性值括号内提取数据?

11
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文