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

  • Home
  • SEARCH
  • 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 6138695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:53:55+00:00 2026-05-23T17:53:55+00:00

In rails 3, how to create a Dropdown from hash I have following code

  • 0

In rails 3, how to create a Dropdown from hash

I have following code in my User class

class User
  ...   other codes
  key :gender, Integer    # i use mongo db

  class << self
    def genders()
      genders = {
        '1' => 'Male',
        '2' => 'Female',
        '3' => 'Secret'
      }
    end
  end

end

In the user form, i am trying to create a gender dropdown list

<%= f.collection_select nil, :gender, User.genders, :key, :value %>

but it complain

undefined method `merge' for :value:Symbol

So what is the proper way to create the dropdown?

Thanks

  • 1 1 Answer
  • 1 View
  • 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-23T17:53:55+00:00Added an answer on May 23, 2026 at 5:53 pm

    This should work:

    <%= f.collection_select :gender, User.genders, :first, :last %>
    

    Edit: Explanations:

    collection_select will call each on the object you give (User.genders here) and the two methods (first and last here) on each object. It’s roughly equivalent to something like this:

    User.genders.each do |object|
      output << "<option value=#{object.first.inspect}>#{h object.last}</option>"
    end
    

    When you call each on a Hash, it yields an Array of two values (the key and the value). These values can be retreived with the first and last methods.

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

Sidebar

Related Questions

I have the following rails migration: create_table :articles do |t| t.integer :user_id, :allow_null =>
I would like to use create a rails route for a user's open id.
Gists of controller code at bottom of page I have a simple rails nesting
When I create a Rails app I am getting some errors and I have
I'm following a tutorial: http://visionmasterdesigns.com/tutorial-create-a-login-system-in-ruby-on-rails/ to create a login page when I try to
I want to make a language selection dropdown in a site user edit/create page.
I have a rails create action which send back some jquery in a file:
Say I have an app with multiple controllers . UserController EventsController Does Rails create
When I add a new product using my scaffold create rails app, the following
I want to create my Rails application with MySQL, because I like it so

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.