2011-09-12 11 views
2

選択した状態に基づいて都市を含むユーザーフォームに動的なドロップダウンがあります。フォームはレコードを作成するときに完全に機能しますが、ユーザーを編集するときには既存の値が入力されます。理想的には、フォームにはユーザーの州と都市が入力されます(州の都市はドロップダウンに表示されます)。形態において編集フォームにあらかじめ入力されていないRailsのダイナミックドロップダウン

、私が持っている:私は部分的に

<%= f.input :state_id, :collection => @states, :prompt => "Pick Your State" %> 
</div>  
<div id="usercities"> 
    <%= render :partial => 'cities', :locals => { :cities => @cities, :user => form } %> 
</div> 

を:コントローラで

<%= simple_fields_for :user do |fields| %> 
    <% if cities.blank? %> 
    <%= fields.input :city_id, :collection => cities, :prompt => "Pick Your State" %> 
    <% else %> 
<%= fields.input :city_id, :collection => cities, :prompt => "Pick Your City" %> 
    <% end %> 
<% end %> 

@states = State.where(:active => true) 
    @cities = City.where(:active => true, :state_id => [@user.state_id]) 

フォームを編集しているとき、都市のドロップダウンは空白ですが、状態を変更すると都市が正しく表示されます。

ありがとうございます!

編集:

わからない、それは関連だが、routes.rbをして、私は 一致している場合: 'ユーザー/ update_city_select/IDを':コントローラ=> 'ユーザー'、:アクション=> 'update_city_select'

答えて

0

コントローラのeditメソッドが@citiesに入力されていない可能性があります。

あなたが持っている行をnewに追加してみてください。もし作業していなければ<%= raise cities.inspect %>を部分的に入れて内容を見てください。

関連する問題