English 中文(简体)
我如何安排铁路3号,以显示一份国家名单,然后列出属于该国的城市?
原标题:How do I setup a Rails 3 jQuery view to show a list of states, then list cities belonging to that state?

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>

我再次赞赏任何反馈!

最佳回答

StackOverflow 答复

I GOT IT!

这里我所说的话...... 另外,如果你看到更好的方法,请让我知道。

<table class="state_list">
<% @states.in_groups_of(1, false).each do |state| %>
<tr>
<% for state in state %>
<td>    
    <script>
    $(function(){
            $( #city-<%=state.id%> ).hide();
        $( #toggle-<%=state.id%> ).toggle(function(){
            $( #city-<%=state.id%> ).fadeIn(100);
        },
        function(){
            $( #city-<%=state.id%> ).fadeOut(100);
        });
    });

    </script>
<div id="toggle-<%=state.id%>"><h3><%= state.name %><br /></h3></div>
    <% @cities.each do |city| %>
        <% if city.state.id == state.id %>
                    <div id="city-<%=state.id%>"><%= 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>

Everything is working just like I hoped!

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签