How do I display select field values rather than their associated stored integers in ruby on rails?

孤街浪徒 提交于 2020-01-06 08:07:52

问题


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.

A form could look like this:

<select id="profile_marital_status" name="profile[marital_status]"><option value=""> Select</option>
<option value="1">Single</option>
<option value="2" selected="selected">Dating</option>
<option value="3">In relationship</option>
<option value="4">Married</option>
<option value="5">Living Together</option>
<option value="6">Divorced</option>
<option value="7">Separated</option>
<option value="8">Widowed</option></select><br />

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.

To solve this issue I'm currently doing this with helper methods:

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

Then in view:

= get_gender(@profile.gender)
= get_marital_status(@profile.marital_status)

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.

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.

Update:

In my ApplicationsHelper I have full list of countries stored in an array:

 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],

Putting this in my view:

= ApplicationHelper::COUNTRY_AND_ISO_CODE[@profile.country]

Shows this result in my view:

 ["Sudan", 729]

I just need to figure out how to pull out just the country name, or some how filter out everything but the string.

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.

Any ideas?


回答1:


First, your code could be refactored to use case:

def get_gender(number)
  case number
  when 1 then 'Male'
  when 2 then 'Female'
  end
end

def get_marital_status(number)
  case number
  when 1 then 'Single'
  when 2 then 'Dating'
  when 3 then 'In relationship'
  when 4 then 'Married'
  when 5 then 'Living Together'
  when 6 then 'Divorced'
  when 7 then 'Separated'
  when 8 then 'Widowed'
  end
end

Second, the contents of the <option> tag are not submitted with the form, so there's absolutely no way to access them on the server. You have the following options:

  • Leave it the way it is.
  • Change the value stored in the database to the actual string value instead of a number, that way you remove the obfuscation at the cost of some space in your database.
  • Denormalize the field, making a separate table for the values, so the number stored is now the ID in the other table.



回答2:


This was my solution. I created arrays some months back for dealing with the editing of user profile details so I tapped into those arrays and got the result I want using the stored integers from the db.

In my ApplicationsHelper I have full list of countries stored in an array:

 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],

Putting this in my view:

= ApplicationHelper::COUNTRY_AND_ISO_CODE[@profile.country].first

Shows this result in my view:

 United Kingdom


来源:https://stackoverflow.com/questions/10972302/how-do-i-display-select-field-values-rather-than-their-associated-stored-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!