我在has_one属于_关系中有两个模型:
class Facility < ApplicationRecord
has_one :machine
end
class Machine < ApplicationRecord
belongs_to :facility
end
在用于保存机器的_form部分中,我有一个collection_select来选择设施:
<%= form.collection_select :facility, Facility.all, :id, :facility_name,
{prompt: "Select a faclity"}, {class: "form-control"} %>
在MachinesController中,我允许了facility_id,如下所示:
def machine_params
params.require(:facsummary).permit(:facility_id, ....)
end
我认为我做对了所有事情,但是尽管在下拉列表中选择了一个工具,但还是收到错误消息:
“设施必须存在”
我哪里出问题了?
答案 0 :(得分:0)
尝试执行以下操作以添加facility_id
而不是facility
<%= form.collection_select :facility_id, Facility.all, :id, :facility_name, {prompt: "Select a faclity"}, {class: "form-control"} %>
和强参数方法,machine
如下所示
params.require(:machine).permit(:facility_id, ....)
或者尝试以上方法不起作用
<%= form.collection_select(:facility_id, Facility.all, :id, :facility_name, {prompt: 'Select a faclity'}, {:class => 'form-control'}) %>
已通过测试