Im looking for the proper way to build a form for the following data structure:
class Profile < ActiveRecord::Base
attr_accessible :name
has_many :weights
accepts_nested_attributes_for :weights
end
class Tag < ActiveRecord::Base
attr_accessible :name
has_many :weights
end
class Weight < ActiveRecord::Base
attr_accessible :weight, :profile_id, :tag_id
belongs_to :profile
belongs_to :tag
end
In the edit profile form I want to pull in all the weights and allow users to update them. I've been able to do this with nested attributes like so:
<%= form_for [:admin, @profile] do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<div class='weights'>
<%= f.fields_for :weights do |ff| %>
<%= ff.label :weight %>
<%= ff.text_field :weight %>
<% end %>
</div>
<%= f.submit %>
<% end %>
The thing is that I actually want to pull in the title of the associated tag_id on each weights row as well (so people know which weight's tag they are changing). I don't see a way to pull this info in, should I be doing some sort of join before I write this form out? Is this a silly approach?
Thanks everyone
-Neil
You should be able to get at the weight through ff.object
and tag through ff.object.tag.title
. Have you tried this?