# Adding items to a list?

**URL:** <https://www.ruby-forum.com/t/adding-items-to-a-list/113910>\
**Category:** Ruby\
**Created:** [September 6, 2007, 2:41am UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910 "2007-09-06T02:41:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 6, 2007, 2:41am UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/1 "2007-09-06T02:41:42Z")

</div>

hi, i’m trying to work this out and been using a bit of the ‘blog in 15  
minutes’ code to basically add a new item to a list from within the  
‘show’ action of the list.

much like how when in the example blog app you view a post and can add  
comments to it on the ‘show’ page.

here’s the item model

class Item \< ActiveRecord::Base  
validates\_presence\_of :body

belongs\_to :list  
acts\_as\_list :scope =\> :list  
end

and the list model

class List \< ActiveRecord::Base  
validates\_presence\_of :title

belongs\_to :user  
has\_many :items, :order =\> “position”  
end

and within the show list page i’ve got a ‘add item’ form

\<% form\_tag :action =\> “item”, :id =\> @list do %\>  
\<%= text\_field “item”, “body”, ‘size’ =\> 15 %\>  
\<%= submit\_tag “Add” %\>  
\<% end %\>

that fires this action

def item  
@item = List.find(params[:id]).items.create(params[:body])  
flash[:notice] = “added your item.”  
redirect\_to :action =\> “show”, :id =\> params[:id]  
end

it all works pretty well alas no item gets created, any ideas?

pulling my hair out on this one…

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 6, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/2 "2007-09-06T16:12:17Z")

</div>

Hmm, looking around this looks similar,

[http://www.ruby-forum.com/topic/102613#new](http://www.ruby-forum.com/topic/102613#new)

however, where he initially put…

def add\_comment  
Post.find(params[:id]).comments.create(params[:comment])  
end

was then replaced with…

def add\_comment  
@post = Post.find(params[:id])  
@post.comments.create(params[:comment])  
render :partial=\>‘post\_comments\_list’  
end

which sounds much more right, as currently it looks like it’s going off  
to find the post that your going to attach the comment to but the  
.create method is not firing as it has no object to use.

will try when i get home tonight, fingers crossed.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 6, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/3 "2007-09-06T16:13:49Z")

</div>

also surely when i try it the first way i should be passing an object  
for it to use and not hoping that it’ll use the one that’s currently  
sitting in memory on the view.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 7, 2007, 1:31am UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/4 "2007-09-07T01:31:29Z")

</div>

doofus!!!

bad dobby! bad bad bad!!!

----\>\>\> @post.comments.create(params[:comment])

urgh!!!

and i was doing…

----\>\>\> @list.items.create(params[:body])

very wrong!!!

why because the param we want to send over isn’t the actual field but  
the object!!!

so…

@list.items.create(params[:item])

works fine now, urgh!!!

at least i learnt something more about ruby and made it use fancy ajax,  
but god does it really show this guy needs some sleep!!!

…off to slam head into nearest wall

😉

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 7, 2007, 1:35am UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/5 "2007-09-07T01:35:57Z")

</div>

John G. wrote:

> doofus!!!
> 
> bad dobby! bad bad bad!!!
> 
> ----\>\>\> @post.comments.create(params[:comment])
> 
> urgh!!!
> 
> and i was doing…
> 
> ----\>\>\> @list.items.create(params[:body])
> 
> very wrong!!!
> 
> why because the param we want to send over isn’t the actual field but  
> the object!!!
> 
> so…
> 
> @list.items.create(params[:item])
> 
> works fine now, urgh!!!
> 
> at least i learnt something more about ruby and made it use fancy ajax,  
> but god does it really show this guy needs some sleep!!!
> 
> …off to slam head into nearest wall
> 
> 😉

Good work. Sometimes the answer is so simple that we need to step back  
before we can see it.

~Jeremy

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 8, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/adding-items-to-a-list/113910/6 "2007-09-08T18:39:18Z")

</div>

Thanks Jeremy, appreciate the comment 😉

Any ideas where I should start for deleting items AJAXically from the  
list?

learning, just sometimes things can be a bit slow,

Jeremy W. wrote:

> John G. wrote:
> 
> > doofus!!!
> > 
> > bad dobby! bad bad bad!!!
> > 
> > ----\>\>\> @post.comments.create(params[:comment])
> > 
> > urgh!!!
> > 
> > and i was doing…
> > 
> > ----\>\>\> @list.items.create(params[:body])
> > 
> > very wrong!!!
> > 
> > why because the param we want to send over isn’t the actual field but  
> > the object!!!
> > 
> > so…
> > 
> > @list.items.create(params[:item])
> > 
> > works fine now, urgh!!!
> > 
> > at least i learnt something more about ruby and made it use fancy ajax,  
> > but god does it really show this guy needs some sleep!!!
> > 
> > …off to slam head into nearest wall
> > 
> > 😉
> 
> Good work. Sometimes the answer is so simple that we need to step back  
> before we can see it.
> 
> ~Jeremy
