0
我試圖根據用戶在上一個菜單中的選擇來填充選擇菜單。我只使用一種模式存儲用戶正在過濾的所有記錄。我的問題是隻獲得唯一的記錄,而不是所有的記錄基於以前的選擇。填充動態選擇菜單與軌道數據庫中的記錄
schools_controller
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
regions.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 %>