如何在条件下禁用复选框? [英] how to disable checkbox on condition?

查看:53
本文介绍了如何在条件下禁用复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在索引页面上创建一个电子表格以显示带有复选框的项目,如果验证 - 复选框将被禁用,以便人们不会错误地勾选它.我想知道我应该用控制器还是视图来做?谢谢:你能帮忙吗?

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

<h1>Scooties 优惠券</h1><%= form_with(url: validate_coupons_path, method: 'patch') 做 |f|%><表格><头><tr><th>有效</th><第>个优惠券<th>赎回</th><th>名字</th><th>姓氏</th><th>电子邮件</th><th>职业</th><th>验证</th><th colspan="3"></th></tr></thead><% @scooties_coupons.each do |scooties_coupon|%><tr><td><%= fields_for('scooties_coupons[]', scooties_coupon) 做 |cf|cf.check_box(:验证)结束%></td><td><%=scooties_coupon.coupon%></td><td><%=scooties_coupon.redeeded%></td><td><%= scooties_coupon.first_name %></td><td><%= scooties_coupon.surname %></td><td><%=scooties_coupon.email %></td><td><%=scooties_coupon.occupation%></td></tr><%结束%></tbody>
<%= f.submit %><%结束%><br>

在 app/controllers/scotties_coupons_controller.rb 中:

<预><代码>def set_valid_couponsto_valid = params[:scooties_coupons].select do |id, attrs|attrs[:validated] == '1'结尾to_not_valid = params[:scooties_coupons].reject do |id, attrs|attrs[:validated] == '1'结尾ScootiesCoupon.transaction 做ScootiesCoupon.where(id: to_valid.keys, 验证: false).update_all(验证:真)ScootiesCoupon.where(id: to_not_valid.keys, 验证: true).update_all(验证:假)结尾redirect_to action: :index, notice: '验证更新'结尾

解决方案

你可以像

<h1>Scooties 优惠券</h1><%= form_with(url: validate_coupons_path, method: 'patch') 做 |f|%><表格><头><tr><th>有效</th><第>个优惠券<th>赎回</th><th>名字</th><th>姓氏</th><th>电子邮件</th><th>职业</th><th>验证</th><th colspan="3"></th></tr></thead><% @scooties_coupons.each do |scooties_coupon|%><tr><td><%如果为真%>这里的东西<%其他%><%= fields_for('scooties_coupons[]', scooties_coupon) 做 |cf|cf.check_box(:验证)结束%><%结束%></td><td><%=scooties_coupon.coupon%></td><td><%=scooties_coupon.redeeded%></td><td><%= scooties_coupon.first_name %></td><td><%= scooties_coupon.surname %></td><td><%=scooties_coupon.email %></td><td><%=scooties_coupon.occupation%></td></tr><%结束%></tbody>
<%= f.submit %><%结束%><br>

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.I was wondering should i do it with controller or the view? thanks : 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

解决方案

you can do something like

<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>
            <% if true %>
                SOMETHING HERE 
            <% else %>
              <%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
                cf.check_box(:validated)
              end %>

            <% 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>

这篇关于如何在条件下禁用复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆