如何在 ruby​​ on rails 中显示选择字段值而不是它们关联的存储整数? [英] How do I display select field values rather than their associated stored integers in ruby on rails?

查看:55
本文介绍了如何在 ruby​​ on rails 中显示选择字段值而不是它们关联的存储整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我允许我的用户在表单中选择他们的性别和婚姻状况,然后发布该表单,以便他们的选择存储在数据库中.

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.

表单可能如下所示:

<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 />

要访问它并在视图中显示存储的数据,我会这样做@profile.marital_status.问题是它会显示存储在 db 中的内容,这将是一个整数.

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

然后在视图中:

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

但这似乎是错误的,因为当我需要显示用户注册的国家/地区时,我没有看到自己输入带有国家/地区列表的长 if 语句.

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.

必须有一种更实用、更明智的方法来做到这一点.对于如何显示选择字段值的任何解决方案,我将不胜感激,例如男性"而不是存储在数据库中的内容,例如1 或女性"2.

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.

更新:

在我的 ApplicationsHelper 中,我将完整的国家/地区列表存储在一个数组中:

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

在我看来:

= ApplicationHelper::COUNTRY_AND_ISO_CODE[@profile.country]

在我的视图中显示此结果:

 ["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.

几个月前我已经输入了完整的国家/地区名称及其 ISO 代码列表,因此这将节省大量时间.

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.

有什么想法吗?

推荐答案

这是我的解决方案.几个月前我创建了数组来处理用户配置文件详细信息的编辑,所以我利用这些数组并使用数据库中存储的整数得到了我想要的结果.

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.

在我的 ApplicationsHelper 中,我将完整的国家/地区列表存储在一个数组中:

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

在我看来:

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

在我的视图中显示此结果:

 United Kingdom

这篇关于如何在 ruby​​ on rails 中显示选择字段值而不是它们关联的存储整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆