Ruby on rails 以简单形式关联的If/Else-Rails

Ruby on rails 以简单形式关联的If/Else-Rails,ruby-on-rails,ruby-on-rails-4,associations,simple-form,form-for,Ruby On Rails,Ruby On Rails 4,Associations,Simple Form,Form For,我正在创建一个工作公告板,其中每个工作公告都有相关的区域>州>城市。发布可以有任意数量的区域、州或城市组合 我的区域架构如下所示: create_table "regions", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "parent" t.string "name" t.s

我正在创建一个工作公告板,其中每个工作公告都有相关的区域>州>城市。发布可以有任意数量的区域、州或城市组合

我的区域架构如下所示:

  create_table "regions", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "parent"
    t.string   "name"
    t.string   "role"
  end
  def new
    @posting = current_user.recruiter_postings.build
    @major_regions = Region.where(:role => "region")
    @state_regions = Region.where(:role => "state")
    @city_regions = Region.where(:role => "city")
  end
我的
new
操作与我的
posting
控制器类似:

  create_table "regions", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "parent"
    t.string   "name"
    t.string   "role"
  end
  def new
    @posting = current_user.recruiter_postings.build
    @major_regions = Region.where(:role => "region")
    @state_regions = Region.where(:role => "state")
    @city_regions = Region.where(:role => "city")
  end
我正在尝试使用区域、州(其中
state.parent==region.id
)和城市(其中
city.parent==state.id
)创建一个手风琴风格的复选框选择器

例如:

区域使用联接表关联到my Postings ActiveRecord

这是我的
发布
型号:

class Posting < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  belongs_to :recruiter, class_name: "User"
  belongs_to :company
  has_many :interests
  has_many :comments, as: :commentable
  has_and_belongs_to_many :job_types
  has_and_belongs_to_many :industries
  has_and_belongs_to_many :regions
  has_and_belongs_to_many :market_caps
  has_and_belongs_to_many :ownerships
  has_many :users, through: :interests
  acts_as_followable
end
class Region < ActiveRecord::Base
  has_and_belongs_to_many :postings
  has_and_belongs_to_many :news_items
  has_and_belongs_to_many :mkt_ints
  has_and_belongs_to_many :companies
  has_and_belongs_to_many :users
end
在我的
postings/_form.html.haml
文件中,我需要显示所有适当的复选框region>States>Cities,并允许通过关联保存它们:

  %ul.major-region-ul
  - @major_regions.each do |region|
    %li
      = f.label :regions, region.name
      = f.check_box :regions
      - @state_regions.each do |state|
        -if state.parent === region.id
          %ul.state-region-ul
            %li
              = f.label :regions, state.name
              = f.check_box :regions
              - @city_regions.each do |city|
                -if city.parent === state.id
                  %ul.city-region-ul
                    %li
                      = f.label :regions, city.name
                      = f.check_box :regions
当我将它们设置为复选框(如上所示)时,它们会正确显示,但不会保存。当我尝试将它们显示并另存为关联时:

  %ul.major-region-ul
  - @major_regions.each do |region|
    %li
      = f.label :regions, region.name
      = f.association :regions
      - @state_regions.each do |state|
        -if state.parent === region.id
          %ul.state-region-ul
            %li
              = f.label :regions, state.name
              = f.association :regions
              - @city_regions.each do |city|
                -if city.parent === state.id
                  %ul.city-region-ul
                    %li
                      = f.label :regions, city.name
                      = f.association :regions
编辑

帮助解决了这个问题


我得到了#


但是现在它为
if else
语句中的每个成功生成每个
区域
,这仍然是一个我不知道如何处理的大问题,我希望得到帮助

这是我的应用程序跟踪:

app/views/postings/_form.html.haml:13:in `block (2 levels) in _app_views_postings__form_html_haml__3504097481979586058_2278688540'
app/views/postings/_form.html.haml:10:in `block in _app_views_postings__form_html_haml__3504097481979586058_2278688540'
app/views/postings/_form.html.haml:3:in `_app_views_postings__form_html_haml__3504097481979586058_2278688540'
app/views/postings/edit.html.erb:1:in `_app_views_postings_edit_html_erb__478558825236209135_2272129060'
我想不出如何做到这两个。维护协会和2。正确显示选项。我可以做其中一个,但不能同时做两个

我感谢你能提供的任何帮助

如果你需要任何进一步的信息,请告诉我

谢谢