使用`:has_many:through`记录关联处理复选框表单 [英] Handle check box forms with an `:has_many :through` Record Association

查看:129
本文介绍了使用`:has_many:through`记录关联处理复选框表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ruby on Rails 3.0.7,我想在我的视图文件(即新的和编辑表单)使用复选框字段与:has_many:通过记录关联。我看到使用 has_and_belongs_to_many 记录关联可以轻松实现 >与第一次提到的关联?

I am using Ruby on Rails 3.0.7 and I would like to use check box fields in my view files (that is, for new and edit forms) with an :has_many :through Record Association. I seen that kind of things easily implemented using an has_and_belongs_to_many Record Association, but is it possible to make that to work easly with the first mentioned association? If so, how to do that?

推荐答案

Dogbert提供的链接是手动操作的好方法。但是,如果您决定使用简单表单,您可以使用该功能。

The link Dogbert supplied is a good way of doing it manual. If you however decide to use Simple Form you get that functionality build in.

使用前面提到的文章中的例子:

Using the example from the previously mentioned article you would do:

<%= simple_form_for(@user) do |f| %>
  <%= f.association :groups, :as => :check_boxes %>
  <%= f.submit %>
<% end %>

这个例子当然很简单,但它应该工作。祝你好运!

This example is of course very simplified but it should work. Good luck!

这篇关于使用`:has_many:through`记录关联处理复选框表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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