使用 Rails 中的表单批量更新对象 [英] bulk update objects using form in rails
问题描述
我有一个人物模型和一个引号模型.每个人都可以有许多与他们相关的引语,而这些引语又具有与他们相关联的作者、文本、网址之类的内容.实际上,实际上只有一个人拥有引号对象,但所有人都可能拥有一个.
我正在尝试在引号上使用一个简单的批量适度工具.就像这样,用户可以输入引号,管理员可以去获取特定人的所有引号(但实际上,只有一个人会有引号)并选中一个框,该框将布尔值设置为 0 或 1.当引号实际上是显示出来的,它们被这个字段过滤了.
我在尝试创建一个可以批量更新所有这些的表单时真的很困惑.我得到了显示所有这些的表格:
<% form_for :quotes, :html =>{:方法=>:put }, :url =>{:控制器=>"引用", :action =>"bulk_update", } 做 |quote_form|%><% @person.quotes.each 做 |quote|%><p><% fields_for "quotes[id][]",quotes do |fields|%>文本....<%= fields.check_box :approved %><br><br><%结束%></p><%结束%><%= submit_tag '更新' %><%结束%>
我的批量更新控制器中有一个非常通用的东西,它基本上就像一个普通的更新控制器.我不确定要在那里放什么.我可以尝试将所有我可能应该使用的引号放在一起,但无论如何我都不知道如何访问该变量,因为我是从 Person 视图等中执行所有这些操作的.控制器是这样的:
def bulk_update@quote = Quote.find(params[:id])结尾
我想我对应该在那里的 ID 以及应该传递什么感到困惑.个人 ID 是否应该成为其中的一部分?就像我说的,从技术上讲,我并不关心所有报价是否都显示在该页面上,它们不需要按用户排序.
我得到的参数如下所示:
"quotes"=>{"id"=>{"6"=>{"approved"=>"0"}, ..
但它抱怨找不到没有 ID 的报价.我不关心 ID,因为我想批量更新所有 ID.似乎这种特殊的结构会映射几个 ID,但也许(可能)我必须更改我的控制器来弥补这一点,但不确定如何.当我尝试从散列中删除引号时,它也不起作用,并且只将 "id"=>{"6"=>{"approved"=>"0"}, .. 作为我的参数.>
我找到了一个名为 update_all 的语句,但我不知道如何有选择地传递我需要的内容...
我发现以下方法对于错误处理也很灵活:
def bulk_update引号 = params["quotes"]["id"]quote.each 做 |quote_id,quote_attrs|报价 = Quote.find(quote_id)quote.update_attributes(quote_attrs)# 其他感兴趣的逻辑,如错误处理# ...结尾# 不要忘记渲染或重定向到正确的视图结尾
更新
正确的做法是将更新逻辑移动到模型中:
class QuotesController <应用控制器def 批量更新错误 = Quote.bulk_update(params["quotes"]["id"])# 其他感兴趣的逻辑,如错误处理# ...# 不要忘记渲染或重定向到正确的视图结尾结尾类报价
I have a person model and a quotes model. Each person can have many quotes associated with them, which in turn has things like Author, Text, Url associated with them. In practice really only one person has a quotes object but all people could potentially have one.
I'm trying to have a dirt simple bulk moderate tool on quotes. As in, users can type in quotes and an admin can go and have all of the quotes of a particular person (but like in practice, only one will have them) and check a box which sets a boolean approved to 0 or 1. When the quotes are actually displayed, they're filtered by this field.
I'm getting really mixed up trying to create a form that updates these all in bulk. I got the form to display all of them as so:
<% form_for :quotes, :html => { :method => :put }, :url => {:controller => "quote", :action => "bulk_update", } do |quote_form| %>
<% @person.quotes.each do |quote| %>
<p>
<% fields_for "quotes[id][]", quote do |fields| %>
Text....
<%= fields.check_box :approved %><br><br>
<% end %>
</p>
<% end %>
<%= submit_tag 'Update' %>
<% end %>
I just have an incredibly generic thing in my bulk update controller which is basically like a normal update controller. I'm not sure what to put in there. I could try to put all the quotes which I probably should but I don't know how to get to that variable anyway because I'm doing all of this from whithin the Person view etc. The controller is osmething like this:
def bulk_update
@quote = Quote.find(params[:id])
end
I guess I'm getting confused about the ID that should be in there and what should be getting passed in. Should the person ID be part of this at all? Like I said, I don't really care if technically all quotes are displayed on that page, they don't need to be sorted by user.
I'm getting parameters that look like this:
"quotes"=>{"id"=>{"6"=>{"approved"=>"0"}, ..
but it complains about not finding a quote without an ID. I don't care about the ID since I want to bulk update all of them. It seems like this particular stucture would map several ids, but maybe (probably) i have to change my controller to compensate for that, not sure how though. It also doesn't work when I try to remove the quotes par tof the hash and just have"id"=>{"6"=>{"approved"=>"0"}, .. as my parameters.
EDIT: I found a statement called update_all, but I don't know how to selectively pass it what I need...
I found the following approach to be flexible specially for error handling as well:
def bulk_update
quotes = params["quotes"]["id"]
quotes.each do |quote_id,quote_attrs|
quote = Quote.find(quote_id)
quote.update_attributes(quote_attrs)
# Other logic of interest like error handling
# . . .
end
# Don't forget to render or redirect to the proper view
end
Update
The proper way to do this is by moving the update logic to the model:
class QuotesController < ApplicationController
def bulk_update
errors = Quote.bulk_update(params["quotes"]["id"])
# Other logic of interest like error handling
# . . .
# Don't forget to render or redirect to the proper view
end
end
class Quote < ActiveRecord::Base
def bulk_update
errors = {}
quotes.each do |quote_id,quote_attrs|
quote = Quote.find(quote_id)
unless quote.update_attributes(quote_attrs)
errors[quote_id] = qoute.errors
end
end
errors
end
end
这篇关于使用 Rails 中的表单批量更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!