【发布时间】:2019-07-10 01:59:02
【问题描述】:
我正在尝试在索引页面上创建一个电子表格以显示带有复选框的项目,如果经过验证 - 该复选框将被禁用,这样人们就不会误选它。我想知道我应该用控制器还是视图来做?谢谢:你能帮忙吗?
在 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>
在 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
【问题讨论】:
-
您的问题到底是什么?你有具体的问题吗?有错误吗?而且——因为你用 rails 3 和 rails 4 标记了你的问题——你实际使用的是什么版本的 Rails?
-
嗨 @spickermann 抱歉,我实际上使用的是版本 3,我的问题是我应该使用控制器还是视图来执行此操作?谢谢
-
stackoverflow.com/a/56741193/608359 中的评论描述了一些关于验证电子邮件地址的业务逻辑,“当电子邮件被验证时,它将作为 true 存储到 db 中,并且勾选的行将被禁用。”这适用于这个问题吗?
-
嗨@DouglasLovell,是的
标签: ruby ruby-on-rails-3