开发者

check_box_tag - one to many relationship

开发者 https://www.devze.com 2023-03-10 02:51 出处:网络
I have 2 models - batch and submision. Batch has_many :submissions Submission belongs_to :batch My submissions table has a field/column which stores the batch id for each submission,

I have 2 models - batch and submision.

Batch has_many :submissions Submission belongs_to :batch

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

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.

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.

Code for selecting 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.

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 preselect开发者_JAVA技巧ed submissions.

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.

Thanks a lot for your help


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

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

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.

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).

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消