【问题标题】:Rails updating records via nested model forms and delete linksRails 通过嵌套模型表单和删除链接更新记录
【发布时间】:2013-09-21 07:40:38
【问题描述】:

我有两个模型 - FamilyPerson:(使用 Mongoid 和 Rails 3.2.13)

family.rb

attr_accessible :location  
has_many :persons
accepts_nested_attributes_for :persons  

person.rb

attr_accessible :name
belongs_to :family  

FamiliesController 我有:

  def edit
   @family=Family.find(params[:id])
  end

  def update
    @family=Family.find(params[:id])
    @family.update_attributes(params[:family])
  end  

在家庭控制器的 edit.html.erb 中:

<div class="container">
<%= simple_form_for @family do |f| %>
  <%= f.error_messages %>
    <%= f.input :location %>
    <%= f.simple_fields_for :persons do |p| %>
      <%= p.input :name %>
    <%end%>
  <%= f.submit "Submit" %>
<% end %>
</div>  

但它只更新家庭属性,人物属性保持不变。

我如何也更新 Person 的属性?

我还想为每个人添加一个delete 按钮,该按钮将删除相应的人。如何实现?

【问题讨论】:

  • 您是否在控制器的某处使用 params.permit?
  • 它用于过滤哪些参数是允许的,但如果你不使用它,那不是原因。

标签: ruby-on-rails forms nested-forms


【解决方案1】:

尝试在attr_accessible中添加persons_attributes #在family.rb

【讨论】:

  • 我有那个accepts_nested_attributes_for :persons 那我为什么需要这个?
  • 是的,这就像魅力一样。为每个人添加一个单独的delete 按钮怎么样?请参阅最后一行的问题。 是否会为每个人添加一个简单的删除按钮来完成这项工作?
  • B/c accepts_nested_attributes_for :persons 只为您的嵌套模型生成额外的参数。
  • 我认为,delete 按钮将在 persons_attributes 内。它会很好用。
  • 更新好吧,我找到了解决方案here。但这给了我No input found for checkbox 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多