iMarv
iMarv

Reputation: 1

Ruby on Rails - Delete something from an array

I have a question with this code. In this view I iterate over an array but I want to have the option to delete an entry from the array. Here is my code

view

<table>
  <tr>
    <th>Ware</th>
    <th>Anzahl</th>
    <th>Einzelpreis</th>
    <th>Gesamtpreis</th>
  </tr>
  <% counter = 0 %>
    <% $itemarray.each do |iarray| %>
    <tr>
      <% @items.each do |item| %>
        <% if iarray.to_i == item.id %>
          <th> <%= item.name %> </th>
          <th> <%= $anzahlarray[counter] %> </th>
          <th> <%= item.price.round(2) %> € </th>
            <% preistemp = preistemp = item.price * $anzahlarray[counter].to_f %>
          <th> <%= preistemp.round(2) %> € </th>

          <th> <%= link_to 'Show', item_path(item) %> </th>
          <th> <%= link_to 'Destroy', :hidden_param => 'counter' , method: :deleteshoppingcartentry(counter), data: {confirm: 'Are you sure?' }%></th>
        <% end %>
      <% end %>
    </tr>  
    <% counter += 1 %>
  <% end %>
</table>

controller

def deleteshoppingcartentry
   $itemarray.delete_at($entrynumber)
   $anzahlarray.delete_at($entrynumber)

   redirect_to "/orders/new"
end

maybe you could help me out

Upvotes: 0

Views: 87

Answers (1)

user626511
user626511

Reputation:

You can't delete items from a global array variable. Use a database instead. Learn more about Rails' ActiveRecord.

Upvotes: 1

Related Questions