選択した州のいくつかの都市を取得する ajax リクエストがあります。したがって、次のように、simple_form と州と都市の 2 つの選択を使用するフォームがあります。
選択した都市の都市を置き換える方法がわかりません。それが私がやった方法です:
Controller ajax method
def by_state
cities = City.by_state params["state"]
render :json => { :cities => cities }
end
Form
<%= simple_form_for @store, :html => { :class => 'store-filter' } do |f| %>
<table align="center" cellspacing="10" cellpadding="2">
<tr>
<th colspan="2">Store list</th>
</tr>
<tr>
<td>State:</td>
<td>
<%= f.input :state, :collection => State.all, :include_blank => false, :label => false, :input_html => { :id => "state", :name => "state" } %>
</td>
</tr>
<tr>
<td>City:</td>
<td>
<%= f.input :city, :collection => City.find(:all, :conditions => { :state_id => 1 }), :include_blank => false, :label => false, :input_html => { :id => "city", :name => "city" } %>
</td>
</tr>
.
.
.
Ajax
$("#state").change(function() {
var state_id = $(this).val();
$.ajax({
type: "GET",
url: "cities/state/"+state_id,
success: function(data) {
console.log(data); //returns Object with Array of Cities
$("#city").html(data["cities"]); //replaces to nothing city select
}
});
});
そのため、州を選択すると、都市の選択が null に置き換えられます。なんで?ありがとう!