Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:59:48+00:00 2026-06-01T21:59:48+00:00

I have a State model which has_many :cities and a city model which belongs_to

  • 0

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.

I’m sure this can be done using jQuery and without partials, I’m just having trouble coming up with some examples & solutions.

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

Any help would be much appreciated!

EDIT

Ok, so I’ve been expelling jQuery I’ve come up with the following code. I can’t get the states to individually show their respective cities. As is, if you click on any of the states, it only shows the cities for the first one.

Here’s the code:

<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>

Again, I appreciate any feedback!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:59:49+00:00Added an answer on June 1, 2026 at 9:59 pm

    StackOverflow Answer

    I GOT IT!

    Here’s what I did for reference… Also, if you see a better method, please let me know.

    <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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a state model and city model associated with has_many and belongs_to.
I have an event model which has city, state, country as foreign keys. Like
I have a model class Place, which has a field for state, and I
I have got the following models, class City < ActiveRecord::Base belongs_to :state belongs_to :country
i'm trying to create a multilevel list. I have two tables 'State' and 'City'.
In my current rails app, I have a User model which has_one :user_detail. Currently,
I have created a simple state machine model in Enterprise Architect and exported it
I have a model and a column of this model represents a state .
I have a state column in a dataframe and I want to create two
I know that immutable objects always have the same state, the state in which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.