RoR中的Collection_check_box用法

我是铁路的新手。 我有一个运行并以状态完成的进程(五种可能的不同状态)。 该过程按建筑物运行。 (我们从建筑物收集数据。)我试图允许用户配置自己接收具有细粒度控制的通知电子邮件:按建筑物和完成状态。 我正在尝试使用collection_check_boxes来创建一个供用户选择的复选框表,但我甚至不确定collection_check_boxes是为这种情况设计的。 我很乐意在这个问题上听到是或否。

我有以下模块:

class Building < ActiveRecord::Base self.primary_key = 'building_id' has_many :etl_status has_many :email_notifications end class BuildingUserPair < ActiveRecord::Base self.primary_key = "building_user_pairs_id" belongs_to :building belongs_to :user has_many :email_notification_settings end class EmailNotificationSetting < ActiveRecord::Base self.primary_key = "email_notification_settings_id" belongs_to :building_user_pair end class EtlResult < ActiveRecord::Base self.primary_key = 'etl_results_id' # table has 5 rows, with values 1 -5 with the statuses "HUNKY DORY", "MO BETTA", "LIMPING ALONG", "DISMAIL FAILURE" and "UNEXPECTEDLY IDLE" end class EtlResultNotification < ActiveRecord::Base belongs_to :building belongs_to :user end class EtlStatus  :email_notifications end class UsersController < ApplicationController def show @user = User.find(params[:id]) @buildings = Building.active @bup = [] @email_notifications_settings = [] @buildings.each do |bldg| bup = BuildingUserPair.where(user_id: params[:id], building_id: bldg.building_id).first_or_create @email_notifications_settings[bldg.building_id] = EmailNotificationSetting.where(building_user_pairs_id: bup.building_user_pairs_id).all end end my users/show.html.erb contains this:   

etl_result_notification表只有两列,除了它的主键,构建用户对列,然后是数字1-5,它是Etl Results表的外键。 因此,我们的想法是为复选框创建一个新行,如果新选中一个复选框,则删除表中的行。 就像我说的,甚至不确定form_for和collection_check_boxes是否设计用于执行此操作。

我的问题是复选框没有正确初始化。 他们都没有受到控制。 我猜我需要将其他paraemters传递给collection_check_boxes,但我不能想到它们应该是什么。

TIA

我认为你的问题太复杂了,你要做的就是保存一个id列表,这个id列在用户想要获得的电子邮件中。

Rubyist发布了https://stackoverflow.com/a/23340368/1380867女巫是你想要做的一个很好的例子。

你应该创建一个serialize :email_notification_ids

 #MODEL class User < ActiveRecord::Base serialize :email_notification_ids end 

希望这有助于Happy Codding