首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rails 6中更新多条记录

如何在rails 6中更新多条记录
EN

Stack Overflow用户
提问于 2020-06-18 17:28:51
回答 1查看 279关注 0票数 0

我的控制器

代码语言:javascript
复制
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

代码语言:javascript
复制
<%= 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%>
EN

回答 1

Stack Overflow用户

发布于 2020-06-18 19:22:49

我的假设是,您正在谈论控制器中的update操作。看起来update_all就是你要找的方法。

代码语言:javascript
复制
def update
  Industry.update_all(industry_params)
end

这可能是危险的-请注意,这将绕过活动记录验证和回调,因此请确保您确定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62446775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档