Rails 4 - has_and_belongs_to_many 关联的复选框 [英] Rails 4 - checkboxes for has_and_belongs_to_many association

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

问题描述

我最近在让复选框为 Rails 4 中的 has_and_belongs_to_many (HABTM) 关联工作时遇到问题.我能够在几个不同的地方找到有关如何使其正常工作的信息,但我认为最好在 StackOverflow 上的一个地方记录使其正常工作所需的几个简单步骤.

I recently had a problem getting checkboxes to work for a has_and_belongs_to_many (HABTM) association in Rails 4. I was able to find the information on how to get it working correctly in a few disparate places, but thought it would be good to document the few simple steps necessary to get it working correctly in one place here on StackOverflow.

作为一个设置,假​​设一个犬舍模型与 HABTM 关联到处理程序.

As a setup assume a model of Kennel with a HABTM association to Handler.

class Kennel
    has_and_belongs_to_many :handlers
end

推荐答案

这就是你需要为表单做的所有事情:当有内置帮助程序时,不要手动操作.

This is all you need to do for the form: Don't do it manually when there is a built in helper.

<%= form_for @kennel do |f| %>
  <%= f.collection_check_boxes(:handler_ids, Handler.all, :id, :to_s) %>
<% end %>

这篇关于Rails 4 - has_and_belongs_to_many 关联的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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