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

发布于 2024-12-04 05:29:16 字数 231 浏览 0 评论 0原文

如何设置模型,以便字段 project_scoreemployee_scorecompany_score 将其值全部映射到数据库?这就是我的意思:

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

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

How can I set up my Model so fields project_score, employee_score, and company_score all map their values to the database? This is what I mean:

'Low' maps to 1
'High' maps to 2
'Very High' maps to 3
'Urgent' maps to 4

Is there a way to get the textual output in my views, and separately convert that text back into the numbers before saving? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-12-11 05:29:16

我认为您要问的是如下

<%= 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 %>

或者,如果您的 Score 位于包含 idname 列的数据库表中。

<%= form_for @something do |f| %>
  <%= f.select :project_score_id, Score.all.map{|s| [s.name, s.id]} %>
<% end %>

您可以在其中创建与分数的关联

belongs_to :project_score, :class_name => 'Score'

而不是使用整数。这也意味着您将字段更改为 project_score_id、employee_score_id、company_score_id

您还可以创建一个自定义 FormBuilder 来创建某种形式的 score_select让一切变得更容易:http://code.alexreisner.com/articles/form-b​​uilders-in-rails.html

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

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) %>

或者,如果您采用了 Score 表和 belongs_to 关联方法

< %= @something.project_score.name %>

I think what you're asking is as follows

<%= 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 %>

Alternatively, if you had your Scores in a database table with an id and name column.

<%= form_for @something do |f| %>
  <%= f.select :project_score_id, Score.all.map{|s| [s.name, s.id]} %>
<% end %>

Where you would create an association to the score

belongs_to :project_score, :class_name => 'Score'

rather than use the integer. This would also mean you change your fields to project_score_id, employee_score_id, company_score_id

You could also create a custom FormBuilder that would create some form of score_select to make it easier down the road: http://code.alexreisner.com/articles/form-builders-in-rails.html.

When displaying, you'll also need some form of helper method to display the appropriate one:

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) %>

Or if you have took the Score table and belongs_to association approach

<%= @something.project_score.name %>

您可以将模型设置为返回如下所示的内容:

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

Then <%= obj.select Model::FREQUENCY.each { |w| w } %> 在视图中。

You can set-up you model to return something like this:

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

Then <%= obj.select Model::FREQUENCY.each { |w| w } %> in the View.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文