前のメニューでのユーザーの選択に基づいて、選択メニューを設定しようとしています。ユーザーがフィルタリングしているすべてのレコードを保存するモデルを 1 つだけ使用しています。私が抱えている問題は、以前の選択に基づいてすべてのレコードではなく、一意のレコードのみを取得することです。
学校_コントローラー
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
地域.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 %>
Municipalities.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 %>