Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-将表单值映射到数据库_Ruby On Rails_Ruby On Rails 3_Callback - Fatal编程技术网

Ruby on rails Rails-将表单值映射到数据库

Ruby on rails Rails-将表单值映射到数据库,ruby-on-rails,ruby-on-rails-3,callback,Ruby On Rails,Ruby On Rails 3,Callback,如何设置我的模型,使project\u score、employee\u score和company\u score字段的值都映射到数据库?这就是我的意思: “低”映射到1 “高”映射到2 “非常高”映射到3 “紧急”映射到4 有没有办法在我的视图中获得文本输出,并在保存之前将文本分别转换回数字?谢谢。我想你的问题如下 <%= form_for @something do |f| %> <%= f.select :project_score, [["Low", 1], ["

如何设置我的模型,使
project\u score
employee\u score
company\u score
字段的值都映射到数据库?这就是我的意思:

“低”映射到1
“高”映射到2
“非常高”映射到3
“紧急”映射到4


有没有办法在我的视图中获得文本输出,并在保存之前将文本分别转换回数字?谢谢。

我想你的问题如下

<%= form_for @something do |f| %>
  <%= f.select :project_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
  <%= f.select :employee_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
  <%= f.select :company_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
<% end %>
您将在其中创建与分数的关联

属于:project\u score,:class\u name=>“score”

而不是使用整数。这也意味着您将字段更改为
project\u score\u id、employee\u score\u id、company\u score\u id

您还可以创建一个自定义的
FormBuilder
,它将创建某种形式的
score\u select
,以简化后续工作:

显示时,还需要某种形式的帮助器方法来显示相应的方法:

module ScoreHelper
  def score_display(score)
    case score
      when 1
        "Low"
      when 2
        "High"
      when 3
        "Very High"
      when 4
        "Urgent"
    end
  end
end

# in your view
<%= score_display(@something.project_score) %>
模块帮助器
def分数显示(分数)
病例评分
当1
“低”
当2
“高”
当3
“非常高”
当4
“紧急”
结束
结束
结束
#在你看来
或者如果您采用了
评分表,并且
属于
关联方法


您可以设置模型以返回如下内容:

RATINGS = [
  ["Low", 1],
  ["High", 2],
  ["Very High", 3],
  ["Urgent", 4]
]
然后在视图中选择

RATINGS = [
  ["Low", 1],
  ["High", 2],
  ["Very High", 3],
  ["Urgent", 4]
]