我的项目在以下类之间存在多对多关系:
class Project < ApplicationRecord
has_many :project_software_processes, inverse_of: :project, :dependent => :delete_all
has_many :software_processes, through: :project_software_processes
accepts_nested_attributes_for :project_software_processes
attr_accessor :project_attributes
end
...
class SoftwareProcess < ApplicationRecord
has_many :project_software_processes
has_many :projects, through: :project_software_processes
end
-
class ProjectSoftwareProcess < ApplicationRecord
belongs_to :software_process
belongs_to :project
end
- -
我正在使用嵌套表单来创建项目实例,以便用户可以根据需要选择任意数量的软件进程:
<div class="field">
<%= f.label :software_processes %>
<%= f.fields_for :project_software_processes do |project_software_process| %>
<%= project_software_process.select(:software_processes_id, @processes.collect { |s| [s.name, s.id] } ) %>
<%= project_software_process.link_to_remove "Remove this SoftwareProcess" %>
<% end %>
<p><%= f.link_to_add "Add a process", :project_software_processes %></p>
</div>
问题是,当我点击提交时,我收到以下验证消息:
项目软件流程必须存在软件流程
我花了一些时间来理解错误的实际含义。我想在尝试创建software process
的实例时抱怨project software process
类的存在。但是,有项目软件过程的实例,用户甚至可以在下拉选择中选择它们。不确定这是否是问题。
这是我项目控制人员的一些片段:
def create
@project = Project.new(project_params)
@project.user = current_user
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
@processes = SoftwareProcess.all
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
-
def project_params
params.require(:project).permit(:name, :user_id, project_software_processes_attributes: [:software_process_id, :_destroy])
end
-
以下是params中发送的内容:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "project"=>{"name"=>"aas", "project_software_processes_attributes"=>{"1484020859023"=>{"software_processes_id"=>"1", "_destroy"=>"false"}, "1484020859693"=>{"software_processes_id"=>"4", "_destroy"=>"false"}}}, "commit"=>"Create Project"}
答案 0 :(得分:1)
问题出在select
方法的名称上,应该是software_process_id
而不是software_processes_id
。尝试将其更改为:
<%= project_software_process.select(:software_process_id, @processes.collect { |s| [s.name, s.id] } ) %>
此外,Ryan Bate的软件现在已经过时了,nested_forms gem也不例外。我见过人们推荐 Cocoon 作为合适的选择。看看here
干杯。