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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:33:27+00:00 2026-05-22T14:33:27+00:00

Rails noob here posing a question. I’m trying to get users to input their

  • 0

Rails noob here posing a question.

I’m trying to get users to input their phone number. Form contains separate fields for countrycode, areacode and phonenumber at the moment.

<% form_tag phones_path, :method => 'get' do %>
<th><%= text_field_tag :countrycode %></th>
<th><%= text_field_tag :areacode %></th>
<th><%= text_field_tag :search, params[:search] %></th>
<th><%= submit_tag "Search", :name => nil %></th>
<% end %>

I want to convert the countrycode field into a dropdown selection of country names, and automatically display the actual country code upon selection. Eg user selections USA, and +1 is displayed. I am going to store both the country name and the numerical code.

I think I need to use a combination of collection_select for the dropdown and javascript to refresh the display. But I’m kinda lost as to how to proceed. Will any kind soul give some hints please?

  • 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-05-22T14:33:27+00:00Added an answer on May 22, 2026 at 2:33 pm

    first you should create a hash in your controller that looks like:

    @countries  = { "United States" => "+1", "Switzerland" => "+41" }
    

    Then create a select list for the countries and add an onchange javascript event handler for the select list:

    <% form_tag phones_path, :method => 'get' do %>
    <th>
      <%= select_tag  :country, 
                      options_for_select(@countries), 
                      :onchange => "document.getElementById('countrycode').value = this.options[selectedIndex].value;" 
      %>
    </th>
    <th><%= text_field_tag :countrycode %></th>
    <th><%= text_field_tag :areacode %></th>
    <th><%= text_field_tag :search, params[:search] %></th>
    <th><%= submit_tag "Search", :name => nil %></th>
    <% end %>
    

    Note: I’ve used standard Javascript for the onchange event, if you use jQuery or prototypeJS this could be written much shorter and cleaner 🙂

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

Sidebar

Related Questions

Rails noob here. I am using carrierwave in a rails 3 app so users
Noob here. I have a rails app that has users and postings. I need
I am a Rails noob and have a question. I have a feed aggregator
This is a noob question: How do I add gems to my rails app
Rails 3.1 I'll simplify my application to get at my question. I have two
Rails noob here so I'm not sure what I'm doing wrong. We're replacing a
Rails noob here looking for some advice/direction on something. I have several Pages and
Rails 3 noob here. Currently the code in my controller below is getting the
Total noob question. I am building a simple photoblog in Rails which consists of
I am a noob in ruby on rails so excuse this question if 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.