本文介绍了通过哈希或数组更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想更新期间模型中的 subject_id 字段。期间循环将位于Form内部。所以,当我提交表单时,传递的参数将作为散列来使用,其中键是 Period.id ,值是 subject_id 即可。因此,如何更新它。 在我的 Show.html.erb 中,我有 <%= form_tag update_institutes_path,方法:: put do%> <%@ period.each do | cc | %GT; <%= cc.subject.name%> <%= collection_select('id',cc.id,Subject.all,:id,:name,selected:cc.id,prompt:true)%> <%end%> <%= submit_tag%><%end%> SHOW行动我有 def show @period = Period.allend $ b 在更新操作中,我有 b def update @period = params [:id] keys,values = @ period.map {| k,v | [k.to_i,v.to_i]} .transpose values.each {| f | 其中 @period = params [:id] 给出一个参数,例如 其中散列中的1和4是period_id 散列中的3和2是subject_id。 然后我将哈希转换为键和值 其中键是period_id 值为subject_id。 然后,我循环使用这些值并更新期间。 它正在更新,但问题只是最后一个值2被更新,其中每个subject_id变为2,我不会没有发生3(第一个值)的情况,只有最后一个值是已更新。我是只是砸我的头,解决这个问题两天。我是新来的铁路,请亲切。感谢您提前给出答案。解决方案你说你有一个散列,其中的键是 period_id ,值为 subject_id 。假设你的散列名称是 period_and_subjects ,你需要这样做: Period.update(period_and_subjects.keys,period_and_subject.values) 所以,如果你的 period_and_subjects 哈希看起来像 {1 => 2,4 => 5} ,它会更新ID为1的期间,ID为2的主体和ID为4的期间ID为5的主体。 p>查看文档 update 。 I want to update the subject_id field in Period Model .Where the period loop will be inside the Form. SO, when I submit the Form the passing Parameter is coming as an hash, where Key is the Period.id and the Value is subject_id. So, How can I update it.In my Show.html.erb I has<%= form_tag update_institutes_path, method: : put do %> <% @period.each do |cc | %> <%= cc.subject.name %> <%= collection_select('id', cc.id, Subject.all, : id, : name, selected: cc.id, prompt: true) %> <% end %> <%= submit_tag %><% end %>The SHow action I hasdef show @period = Period.allendIn the Update action I havedef update @period = params[:id] keys, values = @period.map { |k,v| [k.to_i, v.to_i] }.transpose values.each { |f| Period.all.update_all(:subject_id => f) }endwhere @period = params[:id] give a parameter like #Parameters: {"utf8"=>"✓", "authenticity_token"=>"8SNaUIQ6ZwjYOqBX07QYyVJsbuvYTL2TotSqIg0yj98nYXq5XqJ5XRHqlCvl1aw0lLg9vowyDOl3Hhm+hYDaOA==", "id"=>{"1"=>"3", "4"=>"2"}, "commit"=>"Save changes"}where 1 and 4 in the hash is period_id3 and 2 in the hash is subject_id.Then I converted the hash to keys and values where keys is period_idvalues is subject_id.Then I looped the values and update the Period.It is updating, but the Problem is only the last value 2 is updated, where every subject_id becomes 2, I don't no what happens to 3(the first value), only the last value is updated. I am just banging my head to solve this problem for two days. I am new to rails, Please be kind. Thanks for the answer in advance. 解决方案 You say you have a hash where the keys are period_id and the values are subject_id. Assuming your hash name is period_and_subjects, you want to do this:Period.update(period_and_subjects.keys, period_and_subject.values)So, if your period_and_subjects hash looks like {1 => 2, 4 => 5}, it would update the period with id 1 with the subject with id 2 and the period with id 4 with the subject with id 5.Look at the docs for update. 这篇关于通过哈希或数组更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 18:08