Topic: HTML Table Delete Link

I have a web page with 4 input fields which get stored in the session upon submit. The page posts back to itself and the session data is output in an HTML table.
I have a delete link set up for each row of the table but I can't figure out how to identify the item in the array/cart to delete.
I can set up an input field to manually enter an item number (rownum) as follows but I'd rather handle it by simply clicking the link.
I am new to this and I'm sure my code is a little rough. Please excuse the mess.
Lastly, I've been working from the Agile Web Development book but it gets very confusing for me when straying from the standard database application. Can someone suggest a good beginner resource for non-database backed applications?
Thank you.

session[:cart].items.delete_at(rownum)

My view looks like this:
<table border="1" cellpadding="0">

    <tr>
       <th>GPI</th>
       <th>Plasm</th>
       <th>Member Pay</th>
       <th>Plan Pay</th>
    </tr>
       <%= start_form_tag :action => 'add_claim' %>

       <td><%= text_field_tag( :gpi, @params[:gpi], :size => 14) %></td>
       <td><%= text_field_tag(:plasm, @params[:plasm], :size => 6) %></td>
       <td><%= text_field_tag(:memberpay, @params[:memberpay], :size => 8) %></td>
       <td><%= text_field_tag(:planpay, @params[:planpay], :size => 8) %></td>
       <td><%= submit_tag "Add Claim"%></td>

       <%= end_form_tag %>



<%= start_form_tag :action => 'delete_claim' %>
Delete claim number: <%= text_field_tag(:deleteitem, @params[:deleteitem], :size => 8) %>
<%= submit_tag "Remove Claim"%>
<%= end_form_tag %>

<%= button_to "Remove All" , :action => :empty_cart %>


<table border="1" cellpadding="0">

    <tr>
       <th ALIGN=LEFT>GPI</th>
       <th>Plasm</th>
       <th>Member Pay</th>
       <th>Plan Pay</th>
    </tr>
   
    <% if  not session[:cart].blank? %>

        <% for item in session[:cart].items %>
            <tr>
            <td> <%= h(item[0]) %> </td>
            <td><%= h(item[1]) %></td>
            <td ALIGN=CENTER > <%= h(item[2]) %></td>
            <td ALIGN=CENTER> <%= h(item[3]) %></td>
            <td> <%=h(item.id) %> </td>
            <td><%=link_to "Delete", :controller => "troop", :action => "delete_claim", :id => item.id %>
            </tr>
        <% end %>

    <% end %>   
</table>


Here's my controller:
class TroopController < ApplicationController
 
   def add_claim
    @cart = find_cart                   
    product = [@params[:gpi], @params[:plasm], @params[:memberpay], @params[:planpay]]   
    @cart.add_product(product)     
    redirect_to :action => :troop   
  end
 
  def delete_claim               
    row = @params[:id]   
    session[:cart].items.delete(row)
    redirect_to :action => :troop
  end
 
def empty_cart
  session[:cart] = nil
  flash[:notice] = "Your cart is currently empty"
  redirect_to :action => :troop
end

def item_count
  @cart.items.size
end
 
  private
 
  def find_cart
    session[:cart] ||= Cart.new
  end
 
end


Finally here's my model:
class Cart
  attr_reader :items
 
  def initialize
    @items = []
  end
 
  def add_product(product)
    @items << product
  end
 
  def delete_claim(row)
    @items.delete(row)
  end
 
end

Re: HTML Table Delete Link

rx7haze wrote:

Lastly, I've been working from the Agile Web Development book but it gets very confusing for me when straying from the standard database application. Can someone suggest a good beginner resource for non-database backed applications?

Are you referring to when it stores models in the session instead of the database? I agree, this is one of my biggest problems with the book. If I could change one thing about it, it would be to have the depot app store everything in the database and just reference the id in the session - this is really how it should be. He even mentions in the book if he were making this application for real he would do it this way.

Anyway, since the items aren't being stored in the database, there's no id to reference them. Instead you could either use the index of the array (row number) or the product id. Since a given product can only be added to the cart once, let's use the product id. This way we don't have to rely on the order of the items array.

session[:cart].remove_product(Product.find(params[:id]))

# in cart.rb
def remove_product(product)
    @items.delete_if { |item| item.product == product }
end


Then it's just a matter of passing the product id to the delete action.

<td><%=link_to "Delete", :controller => "troop", :action => "delete_claim", :id => item.product %>

Does that work for you?

Railscasts - Free Ruby on Rails Screencasts

Re: HTML Table Delete Link

There is no unique identifier for the data I'm working with. The same data can be added several times. The purpose of this app is to allow the user to enter information that is not in the database. Ultimately, I will also pull 1 summary record from the database and add to it the sum of the manually entered records stored in the session. I suppose if there is a way to generate a unique identifier I could add that to the array in the session data then later delete based on the unique identifier.
Does rails/ruby make it easy to generate a random number?
Thanks, Bill

Re: HTML Table Delete Link

Yeah, you can use the rand() function to generate a random number, but I don't recommend using that as a unique id since it isn't guranteed to be unique from the other entries. The index of the array should work well enough as long as the order doesn't change.

# view
<% @cart.items.each_with_index do |item, index| %>
  ...
  <td><%=link_to "Delete", :controller => "troop", :action => "delete_claim", :id => index %>
  ...
<% end %>

# controller
session[:cart].items.delete_at(params[:id])

Railscasts - Free Ruby on Rails Screencasts

Re: HTML Table Delete Link

Thank you! Thank you! Thank you!
I can't express how much I appreciate your response. That was exactly the kickstart I needed to finish this thing.