deleting an item with ajax

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

  1. hi again, I managed to work out how to create a new item via ajax and
    the form_remote_tag, however don't know how to delete an item from the
    table.

    here's the code so far, doesn't work, keeps on popping up saying null.id
    found (e.g. it doesn't know which item to destroy).

    show.rhtml

    <ul id='item_list'>
    <%= render :partial=>'item_list' %>
    </ul>


    _item_list.rhtml

    <% for item in @list.items %>
    <li id="item_<%= item.id %>"><%= item.body %>
    <%= link_to_remote "delete", :update => "item_list",
    :url => { :action => "destroy", :id => item.id }
    <%= link_to_remote 'delete', { :action => 'destroy_item', :id =>
    item.id }, :confirm => 'Are you sure?', :method => :post %>
    </li>
    <% end %>



    item.rb (model)

    class Item < ActiveRecord::Base
    #validates_presence_of :body

    belongs_to :list

    acts_as_list :scope => :list
    end


    list.rb (controller)

    def destroy_item
    Item.find(params[:id]).destroy
    render :partial=>'list'
    end


    any ideas?

    think i'm nearly there, just don't know where i'm going wrong?

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

  2. John Griffiths wrote:
    > hi again, I managed to work out how to create a new item via ajax and
    > the form_remote_tag, however don't know how to delete an item from the
    > table.
    >
    > here's the code so far, doesn't work, keeps on popping up saying null.id
    >
    > <ul id='item_list'>
    > <%= render :partial=>'item_list' %>
    > </ul>
    >
    >
    > _item_list.rhtml
    >
    > <% for item in @list.items %>
    > ....
    > <% end %>
    >
    > def destroy_item
    > Item.find(params[:id]).destroy
    > render :partial=>'list'
    > end


    > found (e.g. it doesn't know which item to destroy).

    ..no, the item was destroyed, but when it tried rendering the partial
    again (via ajax Update) there was no item defined, as yo didn't add a
    @list variable, therefore there is no item in the loop, when it is
    rendered secondly.

    add a @list variable to the destroy_item action and u should be ok.
    btw, better to post on the ruby on rails forum (forum#3 rather than
    forum#4)
    --
    Posted via http://www.ruby-forum.com/.
    Shai Rosenfeld, Sep 9, 2007
    #2
    1. Advertising

  3. Thanks Shai, I spent a bit last night going through all my rails books
    trying to piece together in my mind how the form_remote_tag works,
    learnt a lot.

    Understand what your saying and see where I'm going wrong, will try
    changing it.

    And yes, i'll post in the appropriate forum in the future.

    Thanks, your a pal.


    John.

    Shai Rosenfeld wrote:
    > John Griffiths wrote:
    >> hi again, I managed to work out how to create a new item via ajax and
    >> the form_remote_tag, however don't know how to delete an item from the
    >> table.
    >>
    >> here's the code so far, doesn't work, keeps on popping up saying null.id
    >>
    >> <ul id='item_list'>
    >> <%= render :partial=>'item_list' %>
    >> </ul>
    >>
    >>
    >> _item_list.rhtml
    >>
    >> <% for item in @list.items %>
    >> ....
    >> <% end %>
    >>
    >> def destroy_item
    >> Item.find(params[:id]).destroy
    >> render :partial=>'list'
    >> end

    >
    >> found (e.g. it doesn't know which item to destroy).

    > ..no, the item was destroyed, but when it tried rendering the partial
    > again (via ajax Update) there was no item defined, as yo didn't add a
    > @list variable, therefore there is no item in the loop, when it is
    > rendered secondly.
    >
    > add a @list variable to the destroy_item action and u should be ok.
    > btw, better to post on the ruby on rails forum (forum#3 rather than
    > forum#4)


    --
    Posted via http://www.ruby-forum.com/.
    John Griffiths, Sep 9, 2007
    #3
  4. sorted...

    def destroy_item
    Item.find(params[:id]).destroy

    @list = List.find(params[:list])
    render :partial => 'item_list', :eek:bject => @list
    end


    <% for @item in @list.items %>
    <li id="item_<%= @item.id %>"><%= @item.body %>
    <%= link_to_remote "[Destroy]", :update => "items",
    :url => { :action => "destroy_item", :id => @item.id, :list =>
    @item.list_id } %>
    <% end %>



    little did i know in rails you can actually send more than one parameter
    on an action.

    so here, the first is the :id of the item you want to delete, and the
    :list is the list it's connected to, which you'll use to regenerate in
    the show action / page.

    enjoy!


    John Griffiths wrote:
    > Thanks Shai, I spent a bit last night going through all my rails books
    > trying to piece together in my mind how the form_remote_tag works,
    > learnt a lot.
    >
    > Understand what your saying and see where I'm going wrong, will try
    > changing it.
    >
    > And yes, i'll post in the appropriate forum in the future.
    >
    > Thanks, your a pal.
    >
    >
    > John.
    >
    > Shai Rosenfeld wrote:
    >> John Griffiths wrote:
    >>> hi again, I managed to work out how to create a new item via ajax and
    >>> the form_remote_tag, however don't know how to delete an item from the
    >>> table.
    >>>
    >>> here's the code so far, doesn't work, keeps on popping up saying null.id
    >>>
    >>> <ul id='item_list'>
    >>> <%= render :partial=>'item_list' %>
    >>> </ul>
    >>>
    >>>
    >>> _item_list.rhtml
    >>>
    >>> <% for item in @list.items %>
    >>> ....
    >>> <% end %>
    >>>
    >>> def destroy_item
    >>> Item.find(params[:id]).destroy
    >>> render :partial=>'list'
    >>> end

    >>
    >>> found (e.g. it doesn't know which item to destroy).

    >> ..no, the item was destroyed, but when it tried rendering the partial
    >> again (via ajax Update) there was no item defined, as yo didn't add a
    >> @list variable, therefore there is no item in the loop, when it is
    >> rendered secondly.
    >>
    >> add a @list variable to the destroy_item action and u should be ok.
    >> btw, better to post on the ruby on rails forum (forum#3 rather than
    >> forum#4)


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

    deleting item from ListCtrl by pop-up menu

    sendhil kumar, May 29, 2006, in forum: Python
    Replies:
    1
    Views:
    724
    Scott David Daniels
    May 29, 2006
  2. minnie
    Replies:
    1
    Views:
    682
    Andrew Thompson
    Dec 13, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    513
    Alf P. Steinbach
    Apr 19, 2006
  4. mc
    Replies:
    0
    Views:
    730
  5. crea
    Replies:
    2
    Views:
    406
    Nobody
    Dec 28, 2012
Loading...

Share This Page