如何在RoR中找回关系数据?

时间:2010-07-10 14:57:26

标签: ruby-on-rails relationship

我有这样的事情:

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

如果我想编辑员工,可以在此表单中编辑办公室数据吗?

<% 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 个答案:

答案 0 :(得分:1)

你想要的是fields_for

参考:http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

有一个很棒的Railscast,你可以在这里看到:http://railscasts.com/episodes/197-nested-model-form-part-2