adding items to a list?

Discussion in 'Ruby' started by John Griffiths, Sep 6, 2007.

  1. 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...
    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 6, 2007
    #1
    1. Advertising

  2. 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.
    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 6, 2007
    #2
    1. Advertising

  3. 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.
    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 6, 2007
    #3
  4. 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

    ;-)
    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 7, 2007
    #4
  5. John Griffiths 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
    --
    Posted via http://www.ruby-forum.com/.
    Jeremy Woertink, Sep 7, 2007
    #5
  6. 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 Woertink wrote:
    > John Griffiths 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


    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 8, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Molloy
    Replies:
    3
    Views:
    4,748
    nfedin
    Dec 18, 2003
  2. pitcher17

    Adding extra items to a drop down list

    pitcher17, Apr 19, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,139
    Alvin Bruney [MVP]
    Apr 19, 2004
  3. Gilles Kuhn
    Replies:
    0
    Views:
    424
    Gilles Kuhn
    Sep 15, 2003
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    458
    Misbah Arefin
    Feb 14, 2008
  5. divya
    Replies:
    1
    Views:
    1,072
    Munna
    May 28, 2008
Loading...

Share This Page