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 7988027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:19:04+00:00 2026-06-04T12:19:04+00:00

I have gone through N number of solutions for creating a dynamic dropdown in

  • 0

I have gone through N number of solutions for creating a dynamic dropdown in rails but nothing seems to work.

I have a typical state and city model and I do not want to use carmen.

Here is my model :-

Market.rb

class Market < ActiveRecord::Base
    attr_accessible :state, :cities_attributes

    has_and_belongs_to_many :users

    has_many :cities, :dependent => :destroy

    accepts_nested_attributes_for :cities

    validates :state, :presence => true, :uniqueness => true

end

City.rb

class City < ActiveRecord::Base
    attr_accessible :city

    belongs_to :market

    has_and_belongs_to_many :users

end

in devise/registrations/new (I want the user to select the state and city at the time of signup itself)

%li
  =f.collection_select :city, Market.all,:id, :state, html_options = { :class => "custom_textarea"}
%li
 =render "devise/registrations/cities", :f => f

_cities.html.haml partial

-unless cities.blank?
    =f.collection_select( :city, @cities, :id, :cities)

registrations_controller.rb

def update_city_select
    @cities = City.where(:country_id => params[:id])
    render :partial => "cities", :locals => {:cities => @cities}
end

I want the city list to change once the user selects a state and be populated with relevant cities.

How can I achieve this?

  • 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-04T12:19:06+00:00Added an answer on June 4, 2026 at 12:19 pm

    You must use ajax for this in your view which contains the state dropdown:

    $("#stateDropdown").change(function(){
      $.get("controller/update_city_select.html?id="+$("#stateDropdown").val(), 
            function(data){ $("#cityDropdownDiv").html(data); } );
    });
    

    This calls your action, and substitutes the content of your div to the new dropdown for cities.

    To your partial:

    #cityDropdownDiv
        -unless @cities.blank?
            = collection_select( :market, :city, @cities, :id, :cities)
    

    With this, you don’t need to add the :f => f parameter when rendering the partial.

    You should add an id (I am assuming “stateDropdown”) to your dropdown in the html_option part.

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

Sidebar

Related Questions

Have gone through hibernate api specification on FlushMode but didn't get the exact difference.
I have gone through this and this , but the question I am asking
I have gone through the tutorials of using network resources in android. But I
I have gone through some other answers, but cannot get the solution to my
I've have gone through stackoverflow for days and googled a lot but can still
i have gone through a number of tutorials to get an idea as to
I have gone through Cuda programming guide but still not clear where does cuda
I have gone through different questions/articles on Message Brokers and ESBs(Even on stackoverflow). Still
I have gone through following question. Convert NSString to NSDictionary It is something different
I have gone through all the msdn stuff for getting started with the SDK

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.