子模型更新时出现质量分配错误

发布于 2024-11-16 15:47:14 字数 1971 浏览 1 评论 0原文

我在通过嵌套表单更新子模型时遇到问题。我在 StackOverflow 上阅读了很多帖子,希望找到解决方案,但没有成功。将 :contact_info_attributes 添加到服务的 attr_accessible 并不能解决此问题。

这是表格。

<% form_for(@service)  do |service| %>
<%= fields_for :contact_info do |c| %>
<p>
  Contact Person: <%= c.text_field :contactPerson %>
</p>
<p>
  Contact Number: <%= c.text_field :contactNumber %>
</p>
<% end %>
<p>
  Issue: <%= service.text_field :issue, :rows => 3 %>
</p>
<p>
  Urgency: <%= service.text_field :urgency %>
</p>
<%= submit_tag "Update" %> 
<% end %>

这是我的 contact_info 模型。

class ContactInfo < ActiveRecord::Base
  has_many :services

  attr_accessible :contactPerson, :contactNumber
end

这是我的服务模式。

 class Service < ActiveRecord::Base
 belongs_to :user

 has_one :contact_info
 accepts_nested_attributes_for :contact_info, :allow_destroy => true

 attr_accessible :issue, :urgency, :contact_info_attributes, :group, :member

 end

这是“更新”操作。

# services_controller.rb

def update
  @service = Service.find(params[:id])
  if @service.update_attributes(params[:service]) && @service.update_attributes(params[:contact_info])
   redirect_to service_path(@service) 
  else
    redirect_to edit
  end
end

每次我尝试更新表单时,表单的服务部分都会顺利进行,但是 :contact_info 总是遇到像这样的批量分配错误。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"ykJt08tkHSzoYgyQu11lnKms1BZ+vM2i/Q0ZOoYDxks=", "contact_info"=>{"contactPerson"=>"Jmes Jor", "contactNumber"=>"3726262"}, "service"=>{"issue"=>"My house is flooded.", "urgency"=>"1"}, "commit"=>"Update", "id"=>"46"}
Service Load (0.8ms)  SELECT "services".* FROM "services" WHERE "services"."id" = 46 LIMIT 1
WARNING: Can't mass-assign protected attributes: contactPerson, contactNumber

帮助?

I'm having problems with updating a child model via nested form. I've read many threads on StackOverflow in hope to finding a solution but no luck there. adding :contact_info_attributes to service's attr_accessible doesn't do anything to remedy this problem.

Here's the form.

<% form_for(@service)  do |service| %>
<%= fields_for :contact_info do |c| %>
<p>
  Contact Person: <%= c.text_field :contactPerson %>
</p>
<p>
  Contact Number: <%= c.text_field :contactNumber %>
</p>
<% end %>
<p>
  Issue: <%= service.text_field :issue, :rows => 3 %>
</p>
<p>
  Urgency: <%= service.text_field :urgency %>
</p>
<%= submit_tag "Update" %> 
<% end %>

Here's my contact_info model.

class ContactInfo < ActiveRecord::Base
  has_many :services

  attr_accessible :contactPerson, :contactNumber
end

Here's my service model.

 class Service < ActiveRecord::Base
 belongs_to :user

 has_one :contact_info
 accepts_nested_attributes_for :contact_info, :allow_destroy => true

 attr_accessible :issue, :urgency, :contact_info_attributes, :group, :member

 end

Here is the 'update' action.

# services_controller.rb

def update
  @service = Service.find(params[:id])
  if @service.update_attributes(params[:service]) && @service.update_attributes(params[:contact_info])
   redirect_to service_path(@service) 
  else
    redirect_to edit
  end
end

Everytime I try to update the form, the service part of the form goes through okay, but :contact_info keeps running into mass assignment error like so.

Parameters: {"utf8"=>"✓", "authenticity_token"=>"ykJt08tkHSzoYgyQu11lnKms1BZ+vM2i/Q0ZOoYDxks=", "contact_info"=>{"contactPerson"=>"Jmes Jor", "contactNumber"=>"3726262"}, "service"=>{"issue"=>"My house is flooded.", "urgency"=>"1"}, "commit"=>"Update", "id"=>"46"}
Service Load (0.8ms)  SELECT "services".* FROM "services" WHERE "services"."id" = 46 LIMIT 1
WARNING: Can't mass-assign protected attributes: contactPerson, contactNumber

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-11-23 15:47:14

您的表单没有嵌套。

代替:

<%= fields_for :contact_info do |c| %>

放置:

<%= service.fields_for :contact_info do |c| %>

Your forms are not nested.

Instead of:

<%= fields_for :contact_info do |c| %>

Put:

<%= service.fields_for :contact_info do |c| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文