Adding items to a list?

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…

Hmm, looking around this looks similar,

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.

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.

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

:wink:

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

:wink:

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

~Jeremy

Thanks Jeremy, appreciate the comment :wink:

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

:wink:

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

~Jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs