导轨:如何有一个嵌套的形式保存到不同的嵌套模型? [英] Rails: How to have a nested form save to different nested models?
问题描述
上下文:
我有有许多项目
A 公司
模式。每个项目
有许多任务
。在公司
有许多员工
。
I have a Company
model that has many projects
. Each project
has many tasks
. The company
has many employees
.
员工
属于公司
并且有许多任务
。这是每个任务一个员工和雇员将只有一个任务
每项目
。
Employee
belongs to Company
and has many tasks
. It is one employee per task and an employee will have only one task
per project
.
模式:
问题:
我建立一个窗体创建一个项目,用户可以添加多个任务
。每个任务都有的小时
指定和员工
执行任务金额
。在提交的形式应该创建一个项目
的记录,一个或多个任务
记录与小时
和 EMPLOYEE_ID
属性和(!)检查员工
名称数据库中已存在或创建一个新的。
I'm building a form to create a project where the user can add multiple tasks
. Each task has an amount of hours
specified and the employee
performing the task
. Upon submission the form should create a single project
record, one or more task
records with hours
and employee_id
attributes and (!) check if the employee
name already exists in the database or create a new one.
我用怎样的形式为拯救苦苦挣扎的 EMPLOYEE_ID
,而不是 employee_name
。
I'm struggling with how to have the form save the employee_id
rather than the employee_name
.
code:
我的格式如下:
<%= simple_nested_form_for @project do |f| %>
<%= f.input :project_details %>
<%= f.simple_fields_for :tasks do |task| %>
<%= task.input :employee_name, input_html: { data: { autocomplete_source: @queried_employee_names.to_json } } %>
<%= task.input :hours %>
<% end %>
<%= f.link_to_add "Add +", :tasks %>
<%= f.button :submit %>
<% end %>
我的控制器:
class TransactionsController < ApplicationController
def new
@project = current_company.projects.build
end
def create
@project = current_company.projects.new(project_params)
if @project.save
employee_save
redirect_to success_url
else
@project = current_company.projects.build
render :new
end
end
# save the employees not yet in the database
def employee_save
@project.tasks.each do |task|
if current_company.employees.where(name: task.employee_name).empty?
current_company.employees.new(name: task.employee_name).save
end
end
end
def project_params
params.require(:project).permit(:project_details, tasks_attributes: [:id, :employee_name, :hours, :_destroy])
end
end
问:
不幸的是,这将保存到任务
表中的 employee_name
,而不是 EMPLOYEE_ID
(加写一个新的 employee_name
到员工表
)。当然,任务
表应该只包含 employee_ids
和员工
表中的名称
。
Unfortunately, this would save to the tasks
table the employee_name
rather than employee_id
(plus writing a new employee_name
to the employees table
). Of course, the tasks
table should only contain the employee_ids
and the employee
table the names
.
对如何处理这有什么建议?
Any suggestions on how to approach this?
推荐答案
我会像表单中添加一个隐藏字段:
I would add a hidden field in the form like:
<%= simple_nested_form_for @project do |f| %>
<%= f.input :project_details %>
<%= f.simple_fields_for :tasks do |task| %>
<%= task.input :employee_name, input_html: { data: { autocomplete_source: @queried_employee_names.to_json } } %>
<%= task.hidden_field :employee_id, '' %>
<%= task.input :hours %>
<% end %>
<%= f.link_to_add "Add +", :tasks %>
<%= f.button :submit %>
<% end %>
本场必须通过Ajax的填补。我看到你有一个自动完成源的:employee_name
输入字段,那么你可以在那里当您选择员工添加事件,并通过Ajax的获得ID,并填补了隐藏与选定员工的id字段,这样你可以很容易地保存这些数据的创建
操作。
This field must be filled through Ajax. I see you have an autocomplete source in your :employee_name
input field, well you can add an event there when you select the employee, and get the id through Ajax and fill the hidden field with the id of the selected employee, that way you can easily save that data in your create
action.
这篇关于导轨:如何有一个嵌套的形式保存到不同的嵌套模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!