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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:08:49+00:00 2026-05-23T07:08:49+00:00

I have a form select statement, like this: = f.select :country_id, @countries.map{ |c| [c.name,

  • 0

I have a form select statement, like this:

= f.select :country_id, @countries.map{ |c| [c.name, c.id] }

Which results in this code:

...
<option value="1">Andorra</option>
<option value="2">Argentina</option>
...

But I want to add a custom HTML attribute to my options, like this:

...
<option value="1" currency_code="XXX">Andorra</option>
<option value="2" currency_code="YYY">Argentina</option>
...
  • 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-23T07:08:49+00:00Added an answer on May 23, 2026 at 7:08 am

    Rails CAN add custom attributes to select options, using the existing options_for_select helper. You almost had it right in the code in your question. Using html5 data-attributes:

    <%= f.select :country_id, options_for_select(
        @countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] }) %>
    

    Adding an initial selection:

    <%= f.select :country_id, options_for_select(
        @countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] }, 
        selected_key = f.object.country_id) %>
    

    If you need grouped options, you can use the grouped_options_for_select helper, like this (if @continents is an array of continent objects, each having a countries method):

    <%= f.select :country_id, grouped_options_for_select(
        @continents.map{ |group| [group.name, group.countries.
        map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] } ] }, 
        selected_key = f.object.country_id) %>
    

    Credit should go to paul @ pogodan who posted about finding this not in the docs, but by reading the rails source. https://web.archive.org/web/20130128223827/http://www.pogodan.com/blog/2011/02/24/custom-html-attributes-in-options-for-select

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

Sidebar

Related Questions

I have this form: <form name=customize> Only show results within <select name=distance id=slct_distance> <option>25</option>
I have a form like this: <form name=mine> <input type=text name=one> <input type=text name=two>
I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
lets say I have this form <form name=myform action=/acms/test/multiselect/blah action=GET> <select name=mytextarea[] multiple=multiple> <option
I have an update statement in this form: declare @v as int update tbl
I have a form in my forms.py that looks like this: from django import
Please look at this query. select name form table Let suppose this returns 100
I need to create a select statment with name and id like this but
I have a form which is like: class Address_info_updateForm(forms.Form): street = forms.CharField(label=_(Street), required=True) street2
Let's say I have an html form. Each input/select/textarea will have a corresponding <label>

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.