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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:24:07+00:00 2026-06-04T09:24:07+00:00

I am trying to use options_from_collection_for_select(collection, value_method, text_method, selected = nil) But I am

  • 0

I am trying to use

options_from_collection_for_select(collection, value_method, text_method, selected = nil)

But I am unable to pass proper parameter to it. What should be type on collection element? (Sample for complete usage will be helpful)

I have to use it with format like Name=AL and ID=23, like it is State name and it Code.

So for using how should I provide the collection object to it.

I have tried with Hash, Map and Array

  • 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-04T09:24:08+00:00Added an answer on June 4, 2026 at 9:24 am

    The 1st parameter of options_from_collection_for_select should be an arbitrary collection (accuratley any objects which has a map method), for example an Array.

    Example with Array of Hashes:

    options_from_collection_for_select([ { :id => 1, :name => 'Foo' }, { :id => 2, :name => 'Bar' } ], 'to_s', 'to_s')
    

    Of course this is a stupid example, because the 2nd parameter is the method for the value and the 3rd is the method of the name. So the method will call the to_s methods on the Hash items to fetch the id and value for the <option/>, so this will produce:

    '<option value="{:id=&gt;1, :name=&gt;&quot;Foo&quot;}">{:id=&gt;1, :name=&gt;&quot;Foo&quot;}</option>\n<option value="{:id=&gt;2, :name=&gt;&quot;Bar&quot;}">{:id=&gt;2, :name=&gt;&quot;Bar&quot;}</option>'
    

    If you provide proper objects, for example ActiveRecord::Base objects:

    options_from_collection_for_select(Article.all, 'id', 'title')
    

    It will produce a better output:

    '<option value="28">FooBar</option>\n<option value="29">BarFoo</option>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the RQuantLib package to calculate greeks for some options but
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
Should I use old synchronized Vector collection, ArrayList with synchronized access or Collections.synchronizedList or
I am trying to use the beforeSubmit option in the jQuery ajaxForm plugin to
I'm trying to use PHP's split() (preg_split() is also an option if your answer
I am trying to use the ASP.NET Web API Self-Host option with Windows authentication
I'm trying to use jQuery to check for the existence of an <option> that
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease

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.