Creating Multiple Child objects on a single form


#1

I’ve been trying to solve this for the last few hours and I have
something that is working now but I’m curious if there isn’t a better
‘right’ way to do it since this seems a bit of a cludge to me, but I
haven’t find a better way to make it work.

The models are as follows:

Goals, which have multiple objectives.
Goals also have multiple evaluations of those objectives through status
models.

So, I’m trying to create a form for doing the evaluations, which ideally
would allow you to set the status of all objectives in a single view.

My view looks like this (trimmed down a bit for easier posting):

<% for objective in @goal.objectives %>
   <label for="status<%= objective.number %>_status">Status:</label>
     <%= text_field 'status' + objective.number.to_s, 'status' %>
     <%= hidden_field 'status' + objective.number.to_s, 

‘objective_id’, :value => objective.id %>
<% end %>

<label for="evaluation_date">
        Evaluation Date:</label>
<%= date_select 'evaluation', 'evaldate', :order => [:day, :month, 

:year ] %>

Comments:
<%= text_area ‘evaluation’, ‘comment’ %>

This produces a set of ‘status’ objects like status1, status2, status3
in params, which I then loop through again in the create function in my
evaluations controller:

def create
@evaluation = Evaluation.new(params[:evaluation])
@goal = Goal.find_by_id(params[:goal_id])
if @evaluation.save
for objective in @goal.objectives
@evaluation.statuses.create(params[‘status’ +
objective.number.to_s])
end
flash[:notice] = ‘Evaluation was successfully created.’
redirect_to :action => ‘show’, :controller => ‘goals’, :id =>
@goal
else
render :action => ‘new’
end
end

So, this works fine, but I’m curious if there is a better way to do
this.


#2

This may help … http://rorsublist.rubyforge.org/

View this message in context:
http://www.nabble.com/Creating+Multiple+Child+objects+on+a+single+form-t1710645.html#a4644890
Sent from the RubyOnRails Users forum at Nabble.com.