lundi 1 juillet 2019

how to disable checkbox on condition?

I am trying to create a spreadsheet on index page to display items with checkbox and if validated - the checkbox will be disabled so that people won't mis-ticked it. can you help?

In app/views/scooties_coupons/index.html.erb

<table class="table table-hover">

<h1>Scooties Coupons</h1>

<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
  <table>
    <thead>
      <tr>
        <th>Valid</th>
        <th>Coupon</th>
        <th>Redeemed</th>
        <th>First name</th>
        <th>Surname</th>
        <th>email</th>
        <th>occupation</th>
        <th>validation</th>
        <th colspan="3"></th>
      </tr>
    </thead>

    <tbody>
      <% @scooties_coupons.each do |scooties_coupon| %>
        <tr>
          <td>
            <%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
              cf.check_box(:validated)
            end %>
          </td>
          <td><%= scooties_coupon.coupon %></td>
          <td><%= scooties_coupon.redeemed %></td>
          <td><%= scooties_coupon.first_name %></td>
          <td><%= scooties_coupon.surname %></td>
          <td><%= scooties_coupon.email %></td>
          <td><%= scooties_coupon.occupation %></td>

        </tr>
      <% end %>
    </tbody>
  </table>
  <%= f.submit %>
<% end %>

<br>

</table>


in app/controllers/scotties_coupons_controller.rb:


def set_valid_coupons
  to_valid = params[:scooties_coupons].select do |id, attrs|
    attrs[:validated] == '1'
  end
  to_not_valid = params[:scooties_coupons].reject do |id, attrs|
    attrs[:validated] == '1'
  end
  ScootiesCoupon.transaction do
    ScootiesCoupon.where(id: to_valid.keys, validated: false).update_all(
      validated:true)
    ScootiesCoupon.where(id: to_not_valid.keys, validated: true).update_all(
      validated:false)
  end
  redirect_to action: :index, notice: 'Validations updated'
end


Aucun commentaire:

Enregistrer un commentaire