0

前のメニューでのユーザーの選択に基づいて、選択メニューを設定しようとしています。ユーザーがフィルタリングしているすべてのレコードを保存するモデルを 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 %>
4

1 に答える 1

0

Ryan の railscast の動的選択メニューhttp://railscasts.com/episodes/88-dynamic-select-menus-revisedをご覧ください。または、dynamic_form gem を見てください。

どちらもうまくいきます。dyanic_form gem は、予想どおり多くのコーディングを不要にし、javascript ではなくより多くの coffescript を使用します。

于 2013-12-16T12:45:26.240 に答える