views:

18

answers:

1

I have something like this:

 class Employee < ActiveRecord::Base
    has_one :office
  end
  class Office < ActiveRecord::Base
    belongs_to :employee    # foreign key - employee_id
  end

If I want to edit the employee, at this form what can I fo to edit the office data?

<% form_for(@employee) do |f| %>
  <%= f.error_messages %>

    <p>
    <%= f.label :employeeName %><br />
    <%= f.text_field :employeeName %>
  </p>   
<!-- what should I add? -->  
   <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>  
+1  A: 

What you want is fields_for.

Reference: http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

There's a great Railscast for this, which you can watch here: http://railscasts.com/episodes/197-nested-model-form-part-2

Jamie Wong