rails 5带有原始DB值的form_tag,并且在表单中新选择之后,两个记录都必须更新 [英] rails 5 form_tag with original DB value and after new selection in form, both records must be updated

查看:165
本文介绍了rails 5带有原始DB值的form_tag,并且在表单中新选择之后,两个记录都必须更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有radio_button_tag的form_tag,它填充了来自数据库的数据。它必须直接指向一个定制的更新操作(update_multiple),其中更新所有那些已在表单中更改的2条记录的布尔列。

I have a form_tag with a radio_button_tag and it populates with data from DB. It must simply be directed to a customised update action(update_multiple) where a boolean column is updated for all those 2 records which have been changed in the form.

比方说,最初表单是从DB填充的,记录1的单选按钮被选中,现在用户将他的选择更改为记录3,然后在提交表单标签时,必须发生两个记录的更新,但问题是提交时的代码,只收集id现在在该组中选择的记录。如何获得该记录的ID也是未选定的,以便我可以一次更新两个记录?

For e.g. say when initially the form was populated from DB with record 1's radio button selected and now user changed his selection to record 3, then at Submit of form tag the update of both records must occur but the problem is the code at submit, only collects id of record which is now selected in that group.How do I get id of that record also which was unselected so that I can update_all for both of them at one go?

如果提交无法处理此操作,那么在填充表单之前,控制器或表单中是否存在一种方法来保留初始选定记录的标识?正如你所看到的,我试着用radio_button_tag收集一个ID数组。

And if Submit cannot handle this action, then is there a way in controller or form to persist the id of the initial selected record before populating the form? As you see, I've tried with collecting an array of ids[] with radio_button_tag.

TIA为你提供帮助。

TIA for your help.

以下是表单代码:

Here's the form code:

<%= form_tag update_multiple_user_cv_attachments_path, method: :put, action: :update_multiple do %>
      <table class="table table-bordered table-striped">
        <thead>
          <tr>
            <th> Select a CV </th>
            <th> Resume Name </th>
          </tr>
        </thead>
        <tbody>
        <% @cv_attachments.each do |cv_attachment| %>
        <%= hidden_field_tag cv_attachment.main, :value => params[:main] %>
          <tr>
            <td><%= radio_button_tag "cv_attachment_ids[]", cv_attachment.id, cv_attachment.main %> </td>
            <td><%= cv_attachment.attachment.file.basename %></td>
          </tr>
        <% end %>
        </tbody>
      </table>
      <br>
      <%= submit_tag "Select Main", :class =>'button' %>
     <% end %>

这是控制器的update_multiple代码。

Here's the controller update_multiple code.

def update_multiple
CvAttachment.update_all(["updated_at=?", Time.now], :id => params[:cv_attachment_ids])

end

end

推荐答案

马戈。这里'我是如何解决它的一部分你使用hidden_​​field的方式。但现在保持这个线程打开,因为我正在做2 DB更新切换同一列。

Many thanks @margo. Here' how I resolved it partly your way of using hidden_field. But for now keeping this thread open as I'm making 2 DB updates for toggle of same column.

<tbody>
          <% @cv_attachments.each do |cv_attachment| %>
            <% if cv_attachment.main %>
                <%= hidden_field_tag "ex_main", cv_attachment.id %>
            <% end %>
            <tr>
              <td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
              <td><%= cv_attachment.attachment.file.basename %></td>
            </tr>
          <% end %>
        </tbody>

以及在控制器中:

and in controller:

def update_main
if request.put?
  if params["ex_main"] != params["new_main"]
    CvAttachment.find(params[:ex_main]).toggle!(:main)
    CvAttachment.find(params[:new_main]).toggle!(:main)
  end
end

这篇关于rails 5带有原始DB值的form_tag,并且在表单中新选择之后,两个记录都必须更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆