check_box_tag - 一对多关系 [英] check_box_tag - one to many relationship

查看:41
本文介绍了check_box_tag - 一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个模型 - 批量和提交.

I have 2 models - batch and submision.

批量 has_many :submissions提交属于_to :batch

Batch has_many :submissions Submission belongs_to :batch

我的提交表有一个字段/列,用于存储每个提交的批次 ID,

My submissions table has a field/column which stores the batch id for each submission,

在我的新批处理表单中,用户可以通过复选框选择提交并保存表单.这会将新创建的批次 ID 与选定的提交相关联.

In my new batch form, user can select submissions via the checkbox and save the form. This will associate the newly created batch id to the selected submissions.

我目前正在使用以下代码来允许用户选择提交并保存表单,这反过来将创建一个新批次并将新批次 ID 与所选提交相关联.

I am currently using the following code to allow user to select submissions and save the form, which in turn will create a new batch and associate the new batch id to the selected submissions.

选择提交的代码:

 <% @subs.each do |submission| %>
        <td><%= check_box_tag "batch[submission_ids][]", submission.SUB_ID, @batch.submissions.include?(submission) %></td>
        <td><%= submission.SUB_ID %></td>
        <td><%= submission.SUB_NAME %></td>
    </tr>
  <%end%>

它似乎工作正常,除了编辑时.

It seems to be working fine, except when editing.

如果我创建一个新批次并将 2 个提交与其关联,然后我编辑该特定批次并取消选择所有提交并保存该批次,则不会保存更改.但是,如果我只取消选择一个提交或选择另一个提交,则更改会被保存.

If i create a new batch and associate 2 submissions with it and then i edit that particular batch and deselect ALL submissions and save the batch, the changes are not saved. However if i deselect just one of the submission or select another submission, the changes are saved.

只有在编辑包含选定提交的批次并在取消选择所有预选提交后尝试保存该批次时,我才会收到此错误.

I get this error only when am editing a batch containing selected submissions and am trying to save that batch after deselecting all preselected submissions.

我真的很困惑,并认为错误可能与 check_box_tag 有关.

Am really confused about this and thought perhaps the error has something to do with the check_box_tag.

如果有人能就此向我提供一些建议,我将不胜感激.

I would be grateful if somebody could provide me with some suggestions concerning this.

非常感谢您的帮助

推荐答案

我遇到过一次同样的问题.问题是未选中的框不会发送.来自:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

I encountered the same problem once. The thing is that unchecked boxes are not send. From: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

陷阱HTML 规范说未选中的复选框不成功,因此 Web 浏览器不会发送它们.[...]"

"Gotcha The HTML specification says unchecked check boxes are not successful, and thus web browsers do not send them. [...] "

虽然说 check_box_tag 应该没问题(http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag),我必须传递 hidden field_id 以防所有复选框都未选中.

Although it's said that check_box_tag should be ok (http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag), I had to pass hidden field_id just in case that all checkbox are unchecked.

这是一般的线索,但我希望它有所帮助.您应该检查日志,以及如何以及是否传递 id(或者在没有 id 时进行不同的处理).

It's general clue, but I hope it helps. You should check in logs, how and if ids are passed (or deal differently when none are out there).

祝你好运!

这篇关于check_box_tag - 一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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