我在编辑嵌套模型时遇到了问题。当我执行下面的代码时,这会在我的WeeklySchedule表中创建另一个条目。
例句:我想在星期一替换9到10名。
WeeklySchedule表:
编辑前:
weekly_schedule_id: 1 monday: 9编辑后:
weekly_schedule_id: 1 monday: 9
weekly_schedule_id: 2 monday: 10代码:
型号:
class Installation < ActiveRecord::Base
accepts_nested_attributes_for :weekly_schedule
belongs_to :weekly_schedule
end
class WeeklySchedule < ActiveRecord::Base
has_one :installation
end表格:
<%= simple_form_for @installation, class: 'form-horizontal' do |f| %>
<%= f.simple_fields_for :weekly_schedule do |w| %>
<%= w.time_field :monday %>
<% end %>
<% end %>主计长:
def update
@installation.update(installation_params)
(...)
end
def edit
@installation = current_user.installations.find_by(:installation_id => params[:id])
endinstallation_params:
{"x"=>"20",
"y"=>"21",
"weekly_schedule_attributes"=>{"monday"=>"10"}}我做错什么了?
发布于 2016-05-24 18:33:52
当您在嵌套模型的强参数中缺少:id时,就会发生这种情况。确保您的installation_params包括如下所示的:id:
def installation_params
params.require(:installation).permit(..., :weekly_schedule_attributes => [:id, :monday, ...])
endhttps://stackoverflow.com/questions/37418931
复制相似问题