xivusr
xivusr

Reputation: 419

Rails form for model with dynamic number of nested :has_many

I have a model for Workshops and a workshop :has_many Schedules I can edit and create a workshop with one attached Schedule easily by adding the build action in the new method:

def new
 @workshop = Workshop.new
 @workshop.schedule.build
end

This gives me 5 associated schedules:

  def new
    @workshop = Workshop.new
    5.times do
      @workshop.schedule.build
    end
  end 

But what I need is to be able to dynamically attach schedules from the front-end, to build one schedule with the workshop by default and each time a user clicks 'add schedule' add a new schedule form to be attached with the Create call.

Can anyone offer some tips on how to handle this the "rails way" - feel like there is a super simple way to handle this that im overcomplicating.

Thanks in advance!

Upvotes: 2

Views: 2147

Answers (1)

Zubin
Zubin

Reputation: 9712

Ryan Bates has done an excellent screencast on this topic. See http://railscasts.com/episodes/197-nested-model-form-part-2.

Upvotes: 5

Related Questions