I have a Report with many nested Value objects:
Report class
class Report < ActiveRecord::Base
attr_accessible :comments
has_many :values
accepts_nested_attributes_for :values
end
Value class
class Value < ActiveRecord::Base
attr_accessible :value, :assessed_user_id, :behaviour_id
belongs_to :assessed_user
belongs_to :behaviour
belongs_to :report
end
I need a form that will accept a Report with its associated matrix of Value objects like so:
| User | Behaviour1 | Behaviour2 | Behaviour3 | | Bob | ___ | ___ | ___ | | Jane | ___ | ___ | ___ | | Jill | ___ | ___ | ___ |
I’m new to rails and I’ve read all about nested attributes and I’ve tried a million variations on semantic_form_for and possibilities with formtastic, but I cannot seem to place the proper value.value objects in their right places or to set their behaviour and assessed_user as hidden fields, but I cannot seem to get this to work.
The following shows what are supposed to be hidden fields and I cannot get my create (follows) to accept it. Also, I don’t see any indexes on the sub-form elements:
<%= semantic_form_for @report do |f| %>
<table>
<tr>
<th>User</th>
<% @behaviours.each do |behaviour| %>
<th><%= behaviour.name %></th>
<% end %>
</tr>
<% index = 0 %>
<% @members.each do |member| %>
<tr>
<td><%= member.first_name %> <%= member.last_name %></td>
<% @behaviours.each do |behaviour| %>
<% @report.values[index].behaviour_id = behaviour.id %>
<% @report.values[index].assessed_user_id = member.id %>
<td>
<%= f.inputs :behaviour_id, :as => :hidden, :for => @report.values[index] %>
<%= f.inputs :assessed_user_id, :as => :hidden, :for => @report.values[index] %>
<%= f.inputs :value, :for => :values, :for => @report.values[index] %>
<% index = index + 1 %>
</td>
<% end %>
</tr>
<% end %>
</table>
<%= f.inputs :comments %>
<%= f.buttons %>
<% end %>
reports_controller.rb
def create
@report = Report.new( params[:report] )
end
You can have a look on this Railscate esp
http://railscasts.com/episodes/196-nested-model-form-part-1