Note that there are some explanatory texts on larger screens.

plurals
  1. PORails dynamic drop-down not pre-populated in edit form
    text
    copied!<p>I have a dynamic drop-down in a user form that contains cities based on the selected state. The form works perfectly when creating the record, but when editing the user, only the state is populated with the existing value - ideally, the form would be populated with the user's state and city (with the state's cities populated in the drop-down.</p> <p>In the form, I have:</p> <pre><code>&lt;%= f.input :state_id, :collection =&gt; @states, :prompt =&gt; "Pick Your State" %&gt; &lt;/div&gt; &lt;div id="usercities"&gt; &lt;%= render :partial =&gt; 'cities', :locals =&gt; { :cities =&gt; @cities, :user =&gt; form } %&gt; &lt;/div&gt; </code></pre> <p>In the partial I have:</p> <pre><code>&lt;%= simple_fields_for :user do |fields| %&gt; &lt;% if cities.blank? %&gt; &lt;%= fields.input :city_id, :collection =&gt; cities, :prompt =&gt; "Pick Your State" %&gt; &lt;% else %&gt; &lt;%= fields.input :city_id, :collection =&gt; cities, :prompt =&gt; "Pick Your City" %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>In the controller</p> <pre><code> @states = State.where(:active =&gt; true) @cities = City.where(:active =&gt; true, :state_id =&gt; [@user.state_id]) </code></pre> <p>When I am editing the form, the cities drop-down is blank, but if I change the state, it becomes populated with the cities correctly. </p> <p>Thanks!</p> <p>edit:</p> <p>not sure if it's relevant, but in the routes.rb I have match 'users/update_city_select/:id', :controller=>'users', :action => 'update_city_select'</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload