2011-09-12 2 views
2

선택한 상태에 따라 도시가 포함 된 사용자 폼에 동적 드롭 다운이 있습니다. 양식은 레코드를 만들 때 완벽하게 작동하지만 사용자를 편집 할 때 상태 만 기존 값으로 채워집니다. 양식에 사용자의 주 및 도시가 채워지는 것이 이상적입니다 (주 도시는 드롭 다운에 채워짐). 형태 편집 양식에 미리 채워지지 않은 레일즈 동적 드롭 다운

, I는있다 : 상기 부분에서

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

I을 가지고 : 컨트롤러의

<%= 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 %>을 부분적으로 넣어 내용을보십시오.