Hi,
I have been watching the last three episodes on railscasts.com in which he goes through dealing with multiple models in one for using fields_for, and virtual attributes. (Ruby on Rails Screencasts - RailsCasts 73). Here is an example of the way they suggest to go about it:
# projects_controller.rb def new @project = Project.new 3.times { @project.tasks.build } end
def create @project = Project.new(params[:project]) if @project.save flash[:notice] = "Successfully created project." redirect_to projects_path else render :action => 'new' end end
# models/project.rb def task_attributes=(task_attributes) task_attributes.each do |attributes| tasks.build(attributes) end end
VIEW <!-- new.rhtml --> <% form_for :project, :url => projects_path do |f| %> <p> Name: <%= f.text_field :name %> </p> <% for task in @project.tasks %> <% fields_for "project[task_attributes]", task do |task_form| %> <p> Task: <%= task_form.text_field :name %> </p> <% end %> <% end %> <p><%= submit_tag "Create Project" %></p> <% end %>
This example uses a one to many relationship. (they go further in future episodes, but for now this will be fine). My issue is that I would like to do this over multiple relationships. For example: what if a task had multiple requirements? How can I do this with still only calling Project.new(params[:project]) and having everything still set automatically?
Thanks for you help!