使用collection_check_boxes的Rails字段

时间:2017-09-18 16:03:05

标签: ruby-on-rails ruby forms

我有三个模型,segment是具有许多segment_timezones和segment_locales的模型。

Segment.rb

class Segment < ApplicationRecord
  belongs_to :core_bot
  has_many :segment_timezones, dependent: :destroy
  has_many :segment_locales, dependent: :destroy
  has_many :segments_to_subscribers, dependent: :destroy

  accepts_nested_attributes_for :segment_locales, allow_destroy: true
  accepts_nested_attributes_for :segment_timezones, allow_destroy: true
end

Segment_timezone.rb

class SegmentTimezone < ApplicationRecord
  belongs_to :segment
end

Segment_locale.rb

class SegmentLocale < ApplicationRecord
  belongs_to :segment
end

我创建了一个包含fields_for的表单,以便为我的细分保存多个时区和区域设置过滤器:

<%= form_for(segment) do |f| %>
...
<%= f.fields_for :segment_timezones do |r| %>
    <div class="fields">
      <label>Filter by Timezone</label><br>
      <%= r.collection_check_boxes :timezone, @bot_users, :timezone, :id, { :multiple => true } do |b| %>
        <%= b.check_box(:type => "check_box") %>
        <%= "UTC "+b.value.to_s %>
        <%= ActiveSupport::TimeZone[b.value.to_f].name %>
      <% end %>
    </div>
  <% end %>

  <%= f.fields_for :segment_locales do |r| %>
    <div class="fields">
      <label>Filter by Spoken Languages</label><br>
      <%= r.collection_check_boxes :locale, @bot_users, :locale, :id, { :multiple => true } do |l| %>
      <%= l.check_box(:type => "check_box") %>
      <%= l.value.to_s %>
      <% end %>
    </div>
  <% end %>

<% end %>

我想要实现的是保存主段,同时为表segment_timezone和segment_locale中的每个复选框保存一个条目。这样我就可以保存用户想要应用于其片段的过滤器。

不幸的是,使用此配置时,仅保存主段,但不保存segment_timezone或segment_locale。

这是我的分段控制器:

def new
    @segment = Segment.new
    @segment.segment_timezones.build
    @segment.segment_locales.build

    @user = current_user
    @bot = CoreBot.find_by_user_id(@user.id)
    @bot_users = BotUser.where(core_bot_id: @bot.id)
  end

def create
    @user = current_user
    @bot = CoreBot.find_by_user_id(@user.id)
    @bot_users = BotUser.where(core_bot_id: @bot.id)

    @segment = Segment.new(segment_params)

    respond_to do |format|
      if @segment.save
        format.html { redirect_to @segment, notice: 'Segment was successfully created.' }
        format.json { render :show, status: :created, location: @segment }
      else
        format.html { render :new }
        format.json { render json: @segment.errors, status: :unprocessable_entity }
      end
    end
  end

def segment_params
      params.require(:segment).permit(:core_bot_id, :name, :gender, :creation_date_start, :creation_date_finish, segment_timezones_attributes: [ :id, :segment_id, :timezone ], segment_locales_attributes: [ :id, :segment_id, :locale ])
    end

目前,我收到以下错误:

  

2个错误禁止保存此细分:   段时区段必须存在   段区域设置段必须存在

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

试试这个

Segment_timezone.rb

class SegmentTimezone < ApplicationRecord
  serialize :timezone
  belongs_to :segment, optional: true
end

Segment_locale.rb

class SegmentLocale < ApplicationRecord
  serialize :locale
  belongs_to :segment, optional: true
end