adding items to a list?

J

John Griffiths

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, :eek:rder => "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...
 
J

John Griffiths

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.
 
J

John Griffiths

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.
 
J

John Griffiths

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

;-)
 
J

Jeremy Woertink

John said:
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
 
J

John Griffiths

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 said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top