samedi 25 juillet 2015

Collection_check_box usage in RoR

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.

I have the following modles:

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>

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.

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




Aucun commentaire:

Enregistrer un commentaire