I have a State
model which has_many :cities
and a city
model which belongs_to :state
.
I want to show a list of States, then when a user clicks on a State make an ajax call to show the cities belonging to that state below.
我确信,这样做可以采用 j,不采取部分行动,我只是带着一些实例和解决办法而感到不安。
Here s what I want it to look like first: - Alabama - Alaska - Arkansas - etc..
Then if someone clicks on Alaska the list would update to: - Alabama - Alaska - Anchorage - Fairbanks - Juneau - Arkansas
任何帮助都值得赞赏!
http://www.ohchr.org。
Ok,因此,我已驱逐了I ve, 提出以下法律。 我可以让各州单独展示各自的城市。 也就是说,如果你点击任何一个州,它只显示第一个城市。
该法典:
<script>
$(function(){
$( #city ).hide();
$( .toggle ).toggle(function(){
$( #city ).fadeIn(100);
},
function(){
$( #city ).fadeOut(100);
});
});
</script>
<%= render "layouts/header"%>
<table class="state_list">
<% @states.in_groups_of(1, false).each do |state| %>
<tr>
<% for state in state %>
<td>
<div class="toggle"><h3><%= state.name %><br /></h3></div>
<% @cities.each do |city| %>
<% if city.state.id == state.id %>
<div id="city"><%= link_to city.name, cities_path(:city_id => city.id) %> (<%= city.posts.where(:published => true).count %>)<br />
<% end %>
<% end %>
</div>
</td>
<% end %>
</tr>
<% end %>
</table>
我再次赞赏任何反馈!