how put an id unique in a button in a rails loop?

37 views Asked by At

I have a grid where the receipts are listed with their information and the display button. The idea is that pressing the button that appears in the actions column (button_tag) sends the information of that specific receipt to javascript, sending the value of the button (which is the id of the receipt that belongs to that row)- The problem is that from javascript (coffescript) I make an alert to see the information that arrives and it always prints the value stored in the button on the screen, but always the first one in the grid. How can I uniquely identify each button?

Code in ruby:

<div class="row " id = "list_receipt">
  <table class="table table-hover">
      <thead>
      <tr>
        <th scope="col">Month</th>
        <th scope="col">Company</th>
        <th scope="col">Signature of conformity</th>
        <th scope="col">Date of signature</th>
        <th scope="col">Actions</th>
      </tr>
    </thead>
    <tbody>
      <% receipts.sort! { |a, b|  b.payment_month <=> a.payment_month }%>
      <% @receipts.each do |r|  %>
        <tr class=<%= row_color(r.consent)%>>
          <th><%= r.month %></th>
          <td><%= r.company_name %></td>
          <td><%= (r.consent ? 'In compliance': 'In disagreement' ) unless r.consent.nil? %></td>
          <td><%= r.date_consent unless r.date_consent.nil? %></td>
          <td>            
            <div class="input-group-btn">
              <%= button_tag 'Button', type: 'button', class: 'btn btn-succes', id:'viewreceipt_btn', value: r.id do %>
                <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
                <%=r.month %>
              <%end%>
            </div>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>

Code in JavaScript:

$(document).on 'click', '#viewreceipt_btn', (event) ->  
    alert $("#viewreceipt_btn").attr("value")

I'm using rails 4.2.5 and ruby 2.2.3p173.

1

There are 1 answers

0
kwerle On