我试图根据上一个菜单中的用户选择来填充选择菜单。我只使用一种模型来存储用户过滤的所有记录。我遇到的问题是仅获取唯一记录,而不是基于先前选择的所有记录。
学校控制器
class SchoolsController < ApplicationController
def region
@user = current_user
@regions = School.select(:region).uniq.order(:region)
end
def municipalities
@municipalities = School.where(:region => params[:region]).uniq
end
end
模型
# == Schema Information
#
# Table name: schools
#
# id :integer not null, primary key
# region :string(255)
# municipality :string(255)
# school :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class School < ActiveRecord::Base
attr_accessible :region, :municipality, :school
has_many :users
end
region.html.erb
<%= form_tag 'municipalities' do %>
<p><%= select_tag(:region, options_from_collection_for_select(@regions, 'region', 'region')) %>
</p><p>
<%= submit_tag 'Nästa', :class=> 'btn btn-primary' %>
</p>
<% end %>
市政当局.html.erb
<%= form_tag 'schools' do %>
<p><%= select_tag(:municipalities, options_from_collection_for_select(@municipalities ,:municipality ,:municipality )) %>
</p><p>
<%= submit_tag 'Nästa', :class=> 'btn btn-primary' %>
</p>
<% end %>