I want to index it with each statement .zip in rails.

Asked 2 years ago, Updated 2 years ago, 42 views

I have a question.This <%@items.zip(@user~) code would like to have an index number.

Does anyone know?

Syntax error.

<%@items.zip(@user_items).with_index do|item,ui,index|%>
  <td class="invert">%=index%>/td>
<%end%>

show.html.erb

<%@items.zip(@user_items).with_index do|item,ui,index|%>
<tr class="rem1">
    <td class="invert">%=index%>/td>
    <td class="invert-image">
        <%=link_to image_tag(item.img.thumb.url||"sushi1.jpg", class:"img-responsive"), item%>
    </td>

    <td class="invert">
      <div class="quantity"> 
         <div class="quantity-select">                           
             <div class="entry value-minus">>nbsp;</div>
             <div class="entry value" id="score-value">%=ui.quantity%>/div>
             <div class="entry value-plus active">>nbsp;</div>
         </div>
        </div>
    </td>
    <td class="invert">%=link_to item.name, item%></td>
    <td class="invert">$<%=link_to item.price, item%>/td>
    <td class="invert">
            <%=link_to image_tag("close_1.png"), item_delete_in_basket_path(item), method::post, data:{confirm:"You sure?"}, class:"ml-2"%>
    </td>
</tr> 
<%end%>

buckets_controller.rb

class BasketsController <ApplicationController
  before_action —Authenticate_user!
  def show 
    # same as application controller
    
    basket = current_user.prepare_basket
    @user_items=basket.basket_items
    @basket_items=@user_items.select(:item_id)
    @items=Item.where(id:@basket_items)
    @total_price=basket.total_price
  end
end

ruby-on-rails ruby

2022-09-30 17:47

1 Answers

I think you can use each_with_index or each.with_index

array1=["el1", "el2", "el3" ]
array2 = [10, 20, 30]
array1.zip(array2).each_with_index{|(array1_el,array2_el),index|
  p"#{array1_el},#{array2_el},#{index}"
  # "el1, 10, 0"
  # "el2, 20, 1"
  # "el3, 30, 2"
}
array1.zip(array2).each.with_index{|(array1_el,array2_el),index|
  p"#{array1_el},#{array2_el},#{index}"
  # the same
}


2022-09-30 17:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.