rails下拉菜单关联没有db表....

时间:2013-02-26 11:18:43

标签: ruby-on-rails drop-down-menu associations rails-activerecord

尊敬的人......

我有这些模特:

class SanctionedPost < ActiveRecord::Base
  attr_accessible :hospital_id, :sanctioned_posts, :designation_id

  column :district_id
  belongs_to:designation
  belongs_to:hospital
  belongs_to:district
  belongs_to:division
end

class Hospital < ActiveRecord::Base
  attr_accessible  :beds, :fax_no, :hospital_name, :phone_no, :district_id, :institution_type_id, :location_id, :division_id, :block_id, :hospital_type_id, :IsAdministrativeLocation, :IsTribal, :latitude, :longitude

  belongs_to:district
  belongs_to:division
  belongs_to:block
  has_many:sanctioned_posts

end

class Division < ActiveRecord::Base
  attr_accessible :division_name, :state_id

  has_many:health_dept_locations
  belongs_to:state
  has_many:districts
  has_many:hospitals

  has_many:sanctioned_posts

  validates_associated :districts
end

我希望能够在我的制裁中创建下拉菜单,以便分区缩小区域,缩小区块缩小到医院...... (所以我不必从百万家医院中选择......)......

我已尝试过http://railscasts.com/episodes/88-dynamic-select-menus-revisedhttp://railscasts.com/episodes/193-tableless-model的所有内容但仍然无济于事......

====================================

我使用简单的形式......这使我能够为医院创建形式完成类似的任务...

<%= f.association :division,label_method: :division_name, value_method: :id, include_blank: false%>

  <%= f.input :district_id do %>
      <%= f.grouped_collection_select :district_id, Division.order(:division_name), :districts, :division_name, :id, :district_name, include_blank: true, label: 'District'  %>
  <% end %>

  <%= f.input :block_id do %>
      <%= f.grouped_collection_select :block_id, District.order(:district_name), :blocks, :district_name, :id, :block_name, include_blank: true, label: 'Block' %>
  <% end %>

(+随附的coffeescript ......)

但是在我目前的情况下,我无法做同样的事情...因为我没有在sanctioned_posts模型中存储division,district,block id ......但是我相信在这种情况下hospital_id会有所帮助.. ..

Thnx非常提前:) ...

此致

1 个答案:

答案 0 :(得分:0)

为什么不使用CoffeeScript和JQuery对索引方法执行REST请求,您可以相应地配置路由? 例如:国家有学校,学校有学生

各州有很多学校,学校有很多学生

选择伊利诺伊州的第一州。伊利诺伊州的ID为11

请求/states/11/schools.json

SchoolsController < ApplicationController
  responds_to :json

  def index
    states = State.find(params[:id})
    respond_with states.schools
  end
end