peresleguine
peresleguine

Reputation: 2403

How to return appropriate nested attribute value in a form (edit action)?

I have following structure:

class Project < ActiveRecord::Base
  has_many :costs, :dependent => :destroy
  accepts_nested_attributes_for :costs, :allow_destroy => true
end

class Cost < ActiveRecord::Base
  belongs_to :project
end

Suppose there are two attributes in Cost model: cost_plan (used in new action) and cost_fact (properly used in edit). I'm tying to do something like this when editing form:

<!-- _cost_fields.erb -->

<div title="<%= value of :cost_plan %>">
  <%= f.label :cost_fact %>
  <%= f.text_field :cost_fact %>    
</div>

I can return value of :cost_plan using hidden_field, but how to return it as title text?

Upvotes: 0

Views: 435

Answers (1)

apneadiving
apneadiving

Reputation: 115531

Just get the object of the form helper back and retrieves it's associated objects directly:

<%= f.object.attribute %>
<%= f.object.costs.first.attribute %>

Upvotes: 1

Related Questions