简体   繁体   中英

How can I get back the relationship data in RoR?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM