0

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

5
  • 2
    what are you trying to delete? Commented Mar 8, 2015 at 15:58
  • Are you using any database? or you just keep your data in global variables? Commented Mar 8, 2015 at 16:13
  • no i in this case i dont use a database. i store it in a global Array variable and want to delete a certan entry from the array. the delete Option is listed with the items but i dont know how i can delete it. Commented Mar 8, 2015 at 17:44
  • Your code is very hard to understand: Where does $itemarray come from and where does @item come from? BTW: In ruby it's note neccesary to use a $ to declare a variable. Then: In your 'destroy'-link you are calling the deleteshoppingcartentry with an attribute counter, but in your definition of this method you assign no attribute. Commented Mar 8, 2015 at 18:00
  • The global variables are filled from the cookies, and I use them in the whole controller, so I declared them global. the attribute was just a test from me, but I am really clueless how i could delete the certain entry from the array. Commented Mar 9, 2015 at 8:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.