[英]How to update multiple record in rails 6
我的 controller
class IndustryController < ApplicationController
def index
@industries=Industry.all
end
def new
@industry=Industry.new
end
def create
@industry = Industry.new(industry_params)
# @industry = Industry.new(params[:name_bef,:name,:status])
# @industry = Industry.new(:name_bef => (params[:name]),:name => (params[:name]), :status => (params[:status]))
if @industry.save
redirect_to industry_index_path
else
render 'new'
end
end
def update
@industry = Industry.all
@industry.each do |i|
@industry.find(i.id)
@industry.update(industry_params)
end
end
private
def industry_params
params.require(:industry).permit(:name, :status)
end
private
def industry_update
params.require(:industry).permit(:name, :status)
end
end
我的 html
<%= form_for @industries, as: :industry, url: industry_path(@industries), method: :patch do |f| %>
<table class="table table-bordered">
<thead class="bg-light">
<tr>
<th scope="col">業種名(変更前)</th>
<th scope="col">業種名</th>
<th scope="col">状態</th>
</tr>
</thead>
<tbody>
<% @industries.each do |industry| %>
<tr>
<td><%= industry.name %></td>
<td>
<div class="form-group">
<%= f.text_field :name, :value => industry.name, :id => industry.id, :class => "form-control" %>
</div>
</td>
<td>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group text-center">
<%= f.submit"登録", :class => " btn btn-success" %>
</div>
<%end%>
我的假设是您正在谈论 controller 中的update
操作。 看起来update_all是您正在寻找的方法。
def update
Industry.update_all(industry_params)
end
这可能很危险 - 请注意,这将绕过活动记录验证和回调,因此请确保您确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.