[英]Create both associated models from the form of the “belongs_to” one (rails)
With the hope that my consecutive questions are not a problem for anyone, I want to ask for your assistance on how to achieve the creation of two models from one form. 希望我连续提出的问题对任何人都没有问题,我想寻求您的协助,以帮助您实现从一种形式创建两个模型的过程。
The two associated models are 两个相关的模型是
class Employee < ActiveRecord::Base
attr_accessible :name
belongs_to :company
attr_accessible :company_id
end
class Company < ActiveRecord::Base
attr_accessible :name
has_many :employees
end
and what I want is to create a company when I am creating an employee and the input company doesn't already exist. 我想要的是在创建员工而输入公司尚不存在时创建公司。 Without the "when the company doesn't already exist" requirement, this is my code:
没有“当公司不存在时”的要求,这是我的代码:
employees/_form.html.erb
<%= simple_form_for(@employee) do |f| %>
<%= simple_form_for(@company) do |cf| %>
<%= f.input :name, label: 'Employee Name', :required => true %>
<%= cf.input :title, label: 'Company Name', :required => true %>
<% end %>
<% end %>
and employees_controller.rb
和
employees_controller.rb
def new
@employee = Employee.new
@company = Company.new
end
[...] [...]
def create
@employee = Employee.new(params[:employee])
@company = Company.new(params[:company])
@employee.company_id = params[:company_id]
respond_to do |format|
if @employee.save
format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
format.json { render json: @employee, status: :created, location: @employee }
else
format.html { render action: "new" }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
This some kind of association between the employee and the company (that of the employee "holding" the company id) but it doesn't create (or, if I understand well, doesn't actually save) the company. 员工与公司之间的这种关联(员工“持有”公司ID的关联),但是它并不能创建(或者,如果我理解得很好,实际上并不能保存)公司。
If I go on and add @company.save
before the id assignment, everything seems okay. 如果我继续在ID分配之前添加
@company.save
,一切似乎都还好。 Is it, however? 是吗? Shouldn't I render the
new company form
and have everything saved after that is submitted? 我不应该提交
new company form
并在提交之后保存所有内容吗?
I have been searching online all day for the solution but in every case the implementation was performed the opposite way: How to create a bunch of employees from a new company form. 我整天都在网上搜索解决方案,但是在每种情况下,实现都是以相反的方式进行的:如何从新的公司形式中创建一批员工。
Okay, after a lot of experimenting, I 've came down to this: 好的,经过大量的实验,我得出以下结论:
employees/_form.html.erb
<%= simple_form_for(@employee) do |f| %>
<%= simple_fields_for(@company) do |cf| %>
<%= f.input :name, label: 'Employee Name', :required => true %>
<%= cf.input :title, label: 'Company Name', :required => true %>
<% end %>
<% end %>
employees_controller.rb
def create
@employee = Employee.new(params[:employee])
@company = Customer.find_or_create_by_name(params[:company][:name])
@company.employees << @employee
@company.save
@employee.save
end
It is slightly more elegant than the answer provided by @RadBrad but also influenced by it. 它比@RadBrad提供的答案稍微优雅一点,但也受到它的影响。
if you need to know how to actually fill in all the attributes of the company
model, follow this 如果您需要了解如何实际填写
company
模型的所有属性,请遵循以下步骤
There are lot's of ways to go, here is one: 有很多方法可以走,这里是一种:
def Employee << ActiveRecord::Base
def title
return nil
end
end
<%= simple_form_for(@employee) do |f| %>
<%= f.input :name, label: 'Employee Name', :required => true %>
<%= f.input :title, label: 'Company Name', :required => true %>
<%= f.submit 'Create'
<% end %>
Then in your EmployeesController: 然后在您的EmployeesController中:
def create
co = Company.find_by_title(params[:employee][:title]).first
unless co
co = Company.new({:company=>params[:employee][:title]})
end
co.employees.build({:name=>params[:employee][:name]});
co.save
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.