【问题标题】:Rails Submitting Multiple Has_Many Attributes via FormRails 通过表单提交多个 Has_Many 属性
【发布时间】:2017-05-17 20:30:51
【问题描述】:

我正在运行带有以下信息的 Rails 5.1 应用程序:

型号

class Company < ApplicationRecord
  has_many :complaints
  accepts_nested_attributes_for :complaints
  validates :name, presence: true
end

class Complaint < ApplicationRecord
  belongs_to :company
  validates :username, :priority, presence: true
end

控制器

class ComplaintController < ApplicationController

  def new
    @company = Company.new
    @company.complaints.build
  end

  def create
    @company = Company.new(company_params)
    respond_to do |format|
      if @company.save
        format.html { redirect_to complaint_url }
      else
        format.html { render :new }
      end
    end
  end

  private

    def company_params
      params.require(:company).permit(:name, complaints_attributes: [:username, :priority])
    end

视图中的表单

<%= form_for @company do |f| %>

  <%= f.label :name, "Company" %>
  <%= f.text_field :name, type: "text" %>

  <%= f.fields_for :complaints do |complaint| %>

    <%= complaint.label :username, "Username" %>
    <%= complaint.text_field :username %>

    <%= complaint.label :priority, "Priority" %>
    <%= complaint.text_field :priority %>

  <% end %>
  <%= f.submit 'Submit' %>
<% end %>

如果我只有一个输入字段用于表单的complaint_attributes 部分(换句话说,只有一个用户名字段和一个优先级字段,如上所示),这很好。

但是,如果我想在表单中有多个用户名/优先级字段,以便我可以在一次提交中提交多个用户名/优先级组合,我发现提交表单只会保存最后一个用户名/优先级值从表格。这种观点的例子是:

<%= form_for @company do |f| %>

  <%= f.label :name, "Company" %>
  <%= f.text_field :name, type: "text" %>

  <%= f.fields_for :complaints do |complaint| %>

    <div>
      <%= complaint.label :username, "Username" %>
      <%= complaint.text_field :username %>

      <%= complaint.label :priority, "Priority" %>
      <%= complaint.text_field :priority %>
    </div>

    <div>
      <%= complaint.label :username, "Username" %>
      <%= complaint.text_field :username %>

      <%= complaint.label :priority, "Priority" %>
      <%= complaint.text_field :priority %>
    </div>

  <% end %>
  <%= f.submit 'Submit' %>
<% end %>

我注意到在提交表单时,我得到了这样的哈希(用于提交单个投诉):

{"utf8"=&gt;"✓", "authenticity_token"=&gt;"...", "company"=&gt;{"name"=&gt;"Test", "complaints_attributes"=&gt;{"0"=&gt;{"username"=&gt;"test_person", "priority"=&gt;"1"}}}, "commit"=&gt;"Submit"}

有没有办法修改参数使其与此类似并将其保存到数据库中?:

{"utf8"=&gt;"✓", "authenticity_token"=&gt;"...", "company"=&gt;{"name"=&gt;"Test", "complaints_attributes"=&gt;{"0"=&gt;{"username"=&gt;"test_person", "priority"=&gt;"1"}"1"=&gt;{"username"=&gt;"test_person", "priority"=&gt;"2"}}}, "commit"=&gt;"Submit"}

如果不是上述情况,如果在一个表单中为用户名/优先级值使用多个字段,那么保存用户名/优先级值的最佳方法是什么?

编辑:我应该指出,我可以根据需要动态添加用户名/优先级字段组,所以我不想被限制在一个设定的数字。

【问题讨论】:

  • 仅供参考,您的EDIT 使它成为一个完全不同的问题......
  • 是的 - 我很抱歉没有指出这一点。第一个答案发布后我就意识到了。

标签: ruby-on-rails forms


【解决方案1】:

第二个块将覆盖第一个字段...您应该在控制器中构建许多投诉:

def new
  @company = Company.new
  3.times { @company.complaints.build }
end

然后它应该根据您建立的投诉数量使用以下表格生成输入:

<%= form_for @company do |f| %>

  <%= f.label :name, "Company" %>
  <%= f.text_field :name, type: "text" %>

  <%= f.fields_for :complaints do |complaint| %>

    <%= complaint.label :username, "Username" %>
    <%= complaint.text_field :username %>

    <%= complaint.label :priority, "Priority" %>
    <%= complaint.text_field :priority %>

  <% end %>
  <%= f.submit 'Submit' %>
<% end %>

【讨论】:

  • 我试过了,但最后一个块仍然覆盖其他块,因此只保存最后一个块。编辑:无视 - 这有效。但是,它预设了字段的数量。如果我有办法根据需要动态添加字段(我会这样做)怎么办?
  • 奇怪...你能告诉我到达你的控制器的参数吗?
  • 我编辑了我上面的评论。它有效,但如果我有动态字段怎么办?
  • 啊,好吧,在这种情况下,您需要使用 js 构建它(以删除/添加更多关联的对象)......有一些宝石可以帮助你......最流行的是github.com/ncri/nested_form_fieldsgithub.com/nathanvda/cocoon
  • 有趣...让我检查一下,如果这些对我有用,我会告诉你。感谢您的信息。
猜你喜欢
  • 2012-06-27
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 2014-03-25
  • 1970-01-01
相关资源
最近更新 更多