子模型更新时出现质量分配错误
我在通过嵌套表单更新子模型时遇到问题。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的表单没有嵌套。
代替:
放置:
Your forms are not nested.
Instead of:
Put: