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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:01:48+00:00 2026-06-02T04:01:48+00:00

I’m trying to build a dynamic menu so when the user selects a country,

  • 0

I’m trying to build a dynamic menu so when the user selects a country, the state menu is updated.

I’m looking at my dev logs and I see that the action runs the db query and then renders the json.erb file, but I can’t get any response from the browser.

Here’s what I have:

controllers/settings_controller.rb

respond_to :html, :json

  def getstates
    @states = Country.find_by_iso(params[:account][:addresses_attributes]["0"][:country]).states
    respond_with(@states)
  end

(Note: The form is a nested form, that’s why the params hash is so long.)


settings/edit.html.erb

<%= address.collection_select :country, Country.order(:name), :iso, :name, {:include_blank => true}, "data-remote" => true, "data-url" => "/settings/getstates", "data-type" => :json  %>

<%= address.select :state, [], {:include_blank => true}, :disable => true %>

settings/getstates.json.erb

$(document).ready(function() {

    $("#account_addresses_attributes_0_country").bind('ajax:success', function(evt, data, status, xhr){
      var select = $('#account_addresses_attributes_0_state');

      if (data !== null) {
        select.removeAttr('disabled');
        $.each(data, function(key, value) {
          $("<option/>").val(value[1]).text(value[0]).appendTo(select);
        });
      } else {
        select.empty();
        select.attr('disabled', 'disabled');
      }
    });
});

(Note: I got this code from http://blog.madebydna.com/all/code/2011/12/05/ajax-in-rails-3.html)


Dev logs

Processing by SettingsController#getstates as JSON
  Parameters: {"account"=>{"addresses_attributes"=>{"0"=>{"country"=>"UY"}}}}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
  Country Load (0.2ms)  SELECT "countries".* FROM "countries" WHERE "countries"."iso" = 'UY' LIMIT 1
  Rendered settings/getstates.json.erb (0.3ms)
Completed 200 OK in 3ms (Views: 1.5ms | ActiveRecord: 0.3ms)

This is the output I see in my dev logs. But nothing in the browser. I’ve tried adding alerts and console log methods, but nothing.

I’d really appreciate if someone could help me understand what I’m doing wrong.

  • 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-02T04:01:52+00:00Added an answer on June 2, 2026 at 4:01 am

    If you use Firebug’s console tab, you’ll see the ajax request be made. Then you can open it up and see the response body. Like Ryan has suggested you’re requesting JSON but returning full javascript. So if you were to keep it that way you’d have to eval the response in your callback function i.e. eval(data) or jQuery also has a shortcut where you can request a ‘script’ type; But it’s kinda frowned upon now that RJS has been removed from Rails. Instead do as Dan has suggested, move the contents of settings/getstates.json.erb into your edit view, and delete the getstates.json.erb file. To aid you in making this work make the request/response simplier for now. Just do this and inspect it in Firebug to see what’s happening. After which, hook it up to turn the response contents into the option tags you’re wanting.

    $.get("/settings/getstates", function(data){
      console.log(data);
    }, 'json')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

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.