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 /&开发者_开发问答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
精彩评论