Forum: Ruby on Rails Creating Multiple Child objects on a single form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin Kolk (Guest)
on 2006-05-31 17:38
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 ] %>
    <label for="evaluation_comment">
            Comments:</label>
            <%= 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.
puzzelv (Guest)
on 2006-05-31 18:12
(Received via mailing list)
This may help ... http://rorsublist.rubyforge.org/
--
View this message in context:
http://www.nabble.com/Creating+Multiple+Child+obje...
Sent from the RubyOnRails Users forum at Nabble.com.
This topic is locked and can not be replied to.