everyone I have two simple models with embedded one to one relation.

class Employee
  include Mongoid::Document
  field :first_name, type: String
  field :last_name, type: String
  embeds_one :empdate
  accepts_nested_attributes_for :empdate
end

class Empdate
  include Mongoid::Document
  field :hiring_date, type: String
  field :prob_expire, type: String
  embedded_in :employee, :inverse_of => :empdate
end

with the controller methods

  def new
    @employee = Employee.new
    @employee.build_empdate
  end

  def create        
    @employee = Employee.new(params.require(:employee).permit(:first_name, :last_name, :empdate_attributes))
    if @employee.save
      redirect_to employees_path
    else
      render 'new'
    end
  end

and the form for the new employee

<%= form_for @employee do |f| %>
  <%= f.label :first_name, 'First Name:' %>
  <%= f.text_field :first_name %>
  <br />
  <%= f.fields_for :empdate do |d| %>
<%= d.label :hiring_date, 'Hiring Date:' %>
<%= d.text_field :hiring_date %>
  <% end %>
  <%= submit_tag 'submit' %>
<% end %>

After submit, only employee info is saved, but not dates. Can anyone help me with identifying errors ?

有帮助吗?

解决方案

change create method first line to

@employee = Employee.new(params.require(:employee).permit(:first_name, :last_name, :empdate_attributes => [:hiring_date]))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top