# Complex Forms (From Railscasts.com)

**URL:** <https://www.ruby-forum.com/t/complex-forms-from-railscasts-com/117539>\
**Category:** Rails\
**Created:** [October 17, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/complex-forms-from-railscasts-com/117539 "2007-10-17T21:42:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [October 17, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/complex-forms-from-railscasts-com/117539/1 "2007-10-17T21:42:26Z")

</div>

Hi,

I have been watching the last three episodes on [railscasts.com](http://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](http://railscasts.com/episodes/)  
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

\<% form\_for :project, :url =\> projects\_path do |f| %\>

Name: \<%= f.text\_field :name %\>

 \<% for task in @project.tasks %\> \<% fields\_for "project[task\_attributes][]", task do |task\_form| %\> 

Task: \<%= task\_form.text\_field :name %\>

 \<% end %\> \<% end %\> 

\<%= submit\_tag "Create Project" %\>

\<% 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!

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [October 23, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/complex-forms-from-railscasts-com/117539/2 "2007-10-23T17:05:18Z")

</div>

> 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?

I’m having a similar issue… I’m trying to use one form\_for, and  
MULTIPLE fields\_for to set up nested models.

### 

my models are like this (i’m omitting a bunch of stuff):

Post  
:has\_many :groups

Group  
:has\_many :post\_items

### 

I’ve got the second fields for looking something like this:

\<% fields\_for “post[group\_attributes][][post\_item\_attributes][]”,  
post\_item do |post\_item\_form|%\>

 Link Text: \<%= post\_item\_form.text\_field :display\_text %\> URL: \<%= post\_item\_form.text\_field :url %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [October 23, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/complex-forms-from-railscasts-com/117539/3 "2007-10-23T17:16:10Z")

</div>

Alright, I think I have the issue figured out. In your example, i  
think you have 1 too many sets of braces.

For the example above, if a task had multiple requirements, your view  
would look something like the following for the requirements:

\<% for requirement in @project.task.requirements %\>  
\<% fields\_for “project[task\_attributes][requirement\_attributes][]”,  
requirement do |requirement\_form| %\>

Requirement: \<%= requirement\_form.text\_field :name %\>

\<% end %\> \<% end %\>

Then in the task model:

def requirement\_attributes=(requirement\_attributes)  
requirement\_attributes.each do |attributes|  
self.requirements.build(attributes)  
end  
end

This seems to work.
