在RoR中的Collection_check_box使用 [英] Collection_check_box usage in RoR
问题描述
我是一个相对的新手。我有一个运行并完成状态(五种可能的不同状态)的进程。该过程是按建筑物运行的。 (我们从建筑物收集数据。)我试图允许用户配置自己接收通知电子邮件与细粒度控制:每建设和完成状态。我试图使用collection_check_boxes创建一个复选框的表,供用户选择,但我甚至不确定collection_check_boxes是为这种情况设计的。
I'm a relative novice to rails. I have a process that runs and finishes with a status (five possible different statuses). The process is run per-building. (We collect data from buildings.) I'm trying to allow users to configure themselves to receive notification email with fine-grained control: Per-building and complettion status. I'm trying to use collection_check_boxes to create a table of checkboxes for the user to select, but I'm not even sure collection_check_boxes was designed for such a case. I would be very happy to hear a yes or no on that question to start with.
我有以下模式:
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 < ActiveRecord::Base
self.primary_key = 'etl_status_id'
belongs_to :building
has_many :users, :through => :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:
<%= form_for @user do |f| %>
<% @buildings.each do |bldg| %>
<div class="row">
<div class="col-md-4">
<%= bldg.name %>
</div>
<%= f.fields_for :email_notification_settings do |ens| %>
<%= ens.collection_check_boxes( @email_notifications_settings[bldg.building_id],
EtlResult.all, :etl_result_id, :result_name) %>
</div>
<% end %>
<% end %>
<div class="form-buttons">
<%= submit_tag("Update", data: { buildings: @buildings}) %>
</div>
etl_result_notification表只有两列,除了
它是主键,建筑用户-pair列,然后是一个数字,
1-5,它是Etl Results表的外键。因此,想法是一个新行得到
创建一个复选框,如果一个复选框是新取消选中,表中的行被删除。
像我说的,甚至不确定如果form_for和collection_check_boxes甚至设计为这样做。
The etl_result_notification table has just two columns, besides it's primary key, a building-user-pair column and then a number, 1-5 that is a foreign key to the Etl Results table. Thus the idea is that a new line gets created for a checkbox, and if a checkbox is newly unchecked, the row in the table is deleted. Like I said, not even sure if form_for and collection_check_boxes was even designed to do this.
我的问题是复选框没有正确初始化。他们都没有受到控制。
我想我需要传递其他副本到collection_check_boxes,但我不能认为
他们应该是什么。
My problem is that the checkboxes are not being properly initialized. They all come up unchecked. I'm guessing I need to pass other paraemters to collection_check_boxes, but I can't think what they should be.
TIA
TIA
推荐答案
我认为你过于复杂的问题,
你想做的是保存id列表到用户想要得到的电子邮件。
I think that you are over complicating your question, what you want to do is save a list of id that march to the lest of emails the user want to get.
Rubyist发布了 http://stackoverflow.com/a/23340368/1380867女巫是你想做的一个很好的例子。
Rubyist posted http://stackoverflow.com/a/23340368/1380867 witch is a good example of what you want to do.
您应该创建一个 serialize:email_notification_ids
#MODEL
class User < ActiveRecord::Base
serialize :email_notification_ids
end
希望这有助于
Happy Codding
Hope this helps Happy Codding
这篇关于在RoR中的Collection_check_box使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!