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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:39:27+00:00 2026-05-30T01:39:27+00:00

I’m using a select field in a Rails app that is NOT tied to

  • 0

I’m using a select field in a Rails app that is NOT tied to a related model, but stores integer values for a static series of options , i.e.,

<%= select (:this_model, :this_field, [['Option1',1],['Option2',2],['Option3',3],['Option4',4]] ) %>

In a show/ index view, if I want to display the option text (i.e. Option1, Option2, etc) rather than the integer value stored in the database, how do I achieve this?

Thanks for helping a noob learn the ropes!

EDIT

Based on Thorsten’s suggestion below, I implemented the following. But it is returning nil, and I can’t figure out why.

Invoice model:

@@payment_status_data = { 1 => "Pending Invoice" , 2 => "Invoiced" , 3 => "Deposit Received", 4 => "Paid in Full"}

  def text_for_payment_status
     @@payment_status_data[payment_status]
  end

Invoice show view:

Payment Status: <%= @invoice.text_for_payment_status %>

In the console:

irb > i=Invoice.find(4)
=> [#<Invoice id: 4, payment_status: 1 >]
irb > i.text_for_payment_status
=> nil

I’ve tried defining the hash with and without quotes around the keys. What am I missing?

  • 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-30T01:39:29+00:00Added an answer on May 30, 2026 at 1:39 am

    something like this would work:

    <%= form_for @my_model_object do |form| %>
      <%= form.label :column_name "Some Description" %>
      <%= form.select :field_that_stores_id, options_for_select({"text1" => "key1", "text 2" => "key2"}) %>
    <% end %>
    

    Update

    If you later want to display the text you can get it from a simple hash like this:

    {"key1" => "text 1", "key2" => "text2"}[@my_object.field_that_stores_id]
    

    But you better store this hash somewhere in a central place like the model.

    class MyModel < ActiveRecord
      @@my_select_something_data = {"key1" => "text 1", "key2" => "text2"}
      def text_for_something_selectable
        @@my_select_something_data[field_that_stores_id]
      end
    end
    

    Then you can use it in your views like

    @my_object.text_for_something_selectable
    

    There are many possible variations of this. But this should work and you would have all information in a central place.

    Update

    Ok, I used something similar for our website. We need to store return_headers for rma. Those need to store a return reason as a code. Those codes are defined in an external MS SQL Server Database (with which the website exchanges lots of data, like orders, products, and much more). In the external db table are much more return reasons stored than I actually need, so I just took out a few of them. Still must make sure, the codes are correct.

    So here goes he model:

    class ReturnHeader < AciveRecord::Base
      @@return_reason_keys = {"010" => "Wrong Produc",
                              "DAM" => "Damaged",
                              "AMT" => "Wrong Amount"}
    
      def self.return_reason_select
        @@return_reason_keys.invert
      end
    
      def return_reason
        @@return_reason_keys[nav_return_reason_code]
      end
    end
    

    Model contains more code of course, but that’s the part that matters. Relevant here is, that keys in the hash are strings, not symbols.

    In the views i use it like this:

    In the form for edit:

    <%= form_for @return_header do |form| %>
      <%= form.label :nav_return_reason_code "Return Reason" %>
      <%= form.select :nav_return_reason_code, options_for_select(ReturnHeader.return_reason_select, @return_header.nav_return_reason_code) %>
    <% end %>
    

    (Maybe no the most elegant way to do it, but works. Don’t know, why options_for_select expects a hash to be “text” => “key”, but that’s the reason, why above class level method returns the hash inverted.)

    In my index action the return reason is listed in one of the columns. There I can get the value simply by

    @return_headers.each do |rh|
        rh.return_reason
    end
    

    If you have trouble to get it run, check that keys a correct type and value. Maybe add some debug info with logger.info in the methods to see what actual data is used there.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the

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.