1

示されているように、私は3つのモデルを持っています

class Location < ActiveRecord::Base
  attr_accessible :location_name
  has_many :areas
  has_many :restaurants, through: :areas
end

class Area < ActiveRecord::Base
  attr_accessible :area_name, :location_id
  belongs_to :location
  has_many :restaurants
end

class Restaurant < ActiveRecord::Base
  attr_accessible  :description, :menu, :restaurant_name, :area_id
  belongs_to :area
end

単純な形式の宝石を使用しています。新しいレストランを作成し、最初に多くのエリアと、ロケーションに関連付けられた正しいエリアを自動的に選択するロケーションを選択します。次に、1 つの領域に絞り込みます。誰かが大陸を選択してから、特定の大陸の国に絞り込む方法を言うのと同様の概念です。simple_form を使用してこれを達成する方法はありますか? レストラン コントローラーの新しいアクションに追加するものはありますか?

これは、新しいレストランを作成するためのこれまでの私の見解です

<%= simple_form_for @restaurant do |f| %>
<%= f.input :restaurant_name %>
<%= f.input :description %>
<%= f.input :menu %>
<%= f.input :area_id,collection: @locations, as: :grouped_select, group_method: :areas%>
<%= f.button :submit %>
<% end %>

これは期待どおりに機能しません。データベースにロケーションとエリアを既に入力しています。何か案は?

4

1 に答える 1

2

オプションを逆に渡す必要があるのcollectionは、子グループではなく親グループです。あなたの場合、必要なもの:

<%= f.input :area_id,collection: @areas, as: :grouped_select, group_method: :locations %>
于 2013-09-30T19:50:41.950 に答える