0

希望我连续的问题对任何人都没有问题,我想请你帮忙,如何实现从一个表格创建两个模型。

两个相关的模型是

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

当我创建员工并且输入公司尚不存在时,我想要创建一家公司。没有“当公司不存在时”的要求,这是我的代码:

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 %>

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

这种员工和公司之间的某种关联(员工“持有”公司 ID)但它并没有创建(或者,如果我理解得很好,实际上并没有拯救)公司。

If I go on and add @company.save before the id assignment, everything seems okay. Is it, however? Shouldn't I render the new company form and have everything saved after that is submitted?

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.

4

2 回答 2

3

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.

if you need to know how to actually fill in all the attributes of the company model, follow this

于 2013-04-08T20:26:00.950 回答
1

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:

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
于 2013-03-30T18:32:21.737 回答