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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:12:20+00:00 2026-06-06T15:12:20+00:00

I need to populate a dropdownlist in ruby on rails with the data in

  • 0

I need to populate a dropdownlist in ruby on rails with the data in an xml document.

The xml document is formatted like so:

<Countries>
    <Country>
        <CountryCode>US</CountryCode>
        <CountryName>United States</CountryName>
    </Country>
    ...
</Countries>

I then tried to convert to a hash using this code:

<% countries_xml = File.read(".../countries.xml") %>
<% countries = Hash.from_xml(countries_xml) %>

And I got a hash like this:

{"Countries"=>{"Country"=>[{"CountryCode"=>"US", "CountryName"=>"United States"}, ... ]}}

And I’m trying to use this hash to populate the dropdownlist but I think I am stuck. I’ve tried like this:

<% user.select :country, countries["Countries"]["Country"].each %>

But this just fills the dropdownlist with hashes because the countries object is a hash of an array of hashes or something like that. I need it so that the CountryNames are in the dropdownlist and the CountryCodes are sent when the user posts the form.

  • 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-06T15:12:23+00:00Added an answer on June 6, 2026 at 3:12 pm

    Have a look at the documentation for the select form builder method here:
    http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

    Essentially you need to do this to build the array of options for the select:

    countries["Countries"]["Country"].collect { |c| [c['CountryCode'], c['CountryName'] }
    

    Also, you’ll probably want to take that xml file read and parsing methods out of the view and pop it into the controller, or even into the model as a class method. It’ll help to keep your code more maintainable, but that’s just my two cents…

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

Sidebar

Related Questions

I need to populate a .net DataGridView from a collection physically (without data binding)
I ve data in the JSON format given below,need to populate the slick grid
I have a DropDownList. I need populate it with item collected in a List<ListItem>
Hi i need to populate a dropdownlist. I designed a datasource and assigned it
I have a livesearch script that i need to populate my html menu with
I need to create and populate a ListView with 3 strings that come from
i need run code that will create a database and populate tables. i am
I need to create a data.frame that will be populated one row at a
I need to populate a dropdown in ASP.NET MVC 3. I was hoping to
In my datagridview , when in click on datagridviewComboBoxColumn to populate its dropdownlist it

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.