# Nested resources , forms and update

**URL:** <https://www.ruby-forum.com/t/nested-resources-forms-and-update/159314>\
**Category:** Rails\
**Created:** [February 3, 2009, 12:37pm UTC](https://www.ruby-forum.com/t/nested-resources-forms-and-update/159314 "2009-02-03T12:37:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 3, 2009, 12:37pm UTC](https://www.ruby-forum.com/t/nested-resources-forms-and-update/159314/1 "2009-02-03T12:37:47Z")

</div>

Hi I have nested resources i.e. projects which have many tasks and a  
task belongs to a project.

In my project show view I show project info i.e. title and description  
and then list all the tasks associated with it.

I want to have a simple non-javascript form which allows a user to add a  
new task.

e.g.

\<% form\_for @project, :url =\> @project, :method =\> “PUT” do |form| %\>  
\<% fields\_for @project.tasks.first do |task|%\>

Task: \<%= task.text\_field :title %\>

  
\<% end %\>

\<%= submit\_tag "Add Task", :class =\> "submit" %\>

\<% end %\>

In the controller i have this

def show  
@project = Project.find(params[:id])  
#create an empty task for the form  
@project.task.create  
end

Problems -  
Since ive created an empty task for @project, this shows up in my list  
of tasks when it shouldnt. I know i could check all tasks in the view to  
see if they are actually records with .new\_record? but thats adding code  
to the view and i feel there must be a much better solution to this?

I also dont like this \<% fields\_for @project.tasks.first do |task|%\> as  
the .first implies im dealing with many empty tasks waiting to be  
created when in fact there will only be one - is there a better  
workaroudn for this as well?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 3, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/nested-resources-forms-and-update/159314/2 "2009-02-03T12:56:19Z")

</div>

Do @task = @project.tasks.build and use that as the fields\_for argument

On 03/02/2009, at 21:37, Adam A. \<rails-mailing-list@andreas-

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 3, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/nested-resources-forms-and-update/159314/3 "2009-02-03T13:32:36Z")

</div>

ahh i got confused between create and build. And the @task idea, dont  
know why i didnt think about that.

Can i confirm that build doesnt actually add a blank task to @project  
but rather just returns one?

Last im wondering how rest changes where i add code. In the above case  
when a user adds a new task to my project witin the project show view,  
where should i direct to? The create in the task controller or just  
update in the project controller (id create build the new task and then  
update the project model)? Im not sure whether rest encourages you to  
code as much as possible in the parent resource.

Any tips would be greatly appreciated.
