Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I display select field values rather than their associated stored integers in ruby on rails?
    text
    copied!<p>Say for example I allow my users to select their gender and marital status in form and then post that form so their selections are stored in the database.</p> <p><strong>A form could look like this:</strong></p> <pre><code>&lt;select id="profile_marital_status" name="profile[marital_status]"&gt;&lt;option value=""&gt; Select&lt;/option&gt; &lt;option value="1"&gt;Single&lt;/option&gt; &lt;option value="2" selected="selected"&gt;Dating&lt;/option&gt; &lt;option value="3"&gt;In relationship&lt;/option&gt; &lt;option value="4"&gt;Married&lt;/option&gt; &lt;option value="5"&gt;Living Together&lt;/option&gt; &lt;option value="6"&gt;Divorced&lt;/option&gt; &lt;option value="7"&gt;Separated&lt;/option&gt; &lt;option value="8"&gt;Widowed&lt;/option&gt;&lt;/select&gt;&lt;br /&gt; </code></pre> <p>To access this and display the stored data in a view I would do this @profile.marital_status. The problem is it will display what's stored in the db which would be an integer.</p> <p>To solve this issue I'm currently doing this with helper methods:</p> <pre><code>def get_gender(number) if number == 1 'Male' elsif number == 2 'Female' end end def get_marital_status(number) if number == 1 'Single' elsif number == 2 'Dating' elsif number == 3 'In relationship' elsif number == 4 'Married' elsif number == 5 'Living Together' elsif number == 6 'Divorced' elsif number == 7 'Separated' elsif number == 8 'Widowed' end end </code></pre> <p><strong>Then in view:</strong></p> <pre><code>= get_gender(@profile.gender) = get_marital_status(@profile.marital_status) </code></pre> <p>This seems wrong though because when I get to the stage when I need to display back the country the user registered I don't see myself typing out a long if statement with a list of countries.</p> <p>There must be a much more practical sensible way of doing this. I would appreciate any solutions for how I can display back the select field values e.g. "Male" instead of what was stored in the DB e.g. 1 or "Female" 2.</p> <p>Update:</p> <p><strong>In my ApplicationsHelper I have full list of countries stored in an array:</strong></p> <pre><code> COUNTRY_AND_ISO_CODE = [ ['Any', nil], ['United Kingdom', 826], ['United States', 840], ['-----------', ' '], ['Afghanistan', 4], ['Ãland Islands', 248], ['Albania', 8], ['Algeria', 12], ['American Samoa', 16], ['Andorra', 20], ['Angola', 24], ['Anguilla', 660], ['Antarctica', 10], ['Antigua and Barbuda', 28], ['Argentina', 32], ['Armenia', 51], ['Aruba', 533], </code></pre> <p><strong>Putting this in my view:</strong></p> <pre><code>= ApplicationHelper::COUNTRY_AND_ISO_CODE[@profile.country] </code></pre> <p><strong>Shows this result in my view:</strong></p> <pre><code> ["Sudan", 729] </code></pre> <p>I just need to figure out how to pull out just the country name, or some how filter out everything but the string.</p> <p>I already typed out the a full list of country names and their ISO codes a few months ago so this will save a lot of time.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload