开发者

How can I get back the relationship data in RoR?

开发者 https://www.devze.com 2023-01-07 03:39 出处:网络
I have something like this: class Employee < ActiveRecord::Base has_one :office end class Office < ActiveRecord::Base

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 /&开发者_开发问答gt;
    <%= f.text_field :employeeName %>
  </p>   
<!-- what should I add? -->  
   <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>  


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

0

精彩评论

暂无评论...
验证码 换一张
取 消