Rails - 将表单值映射到数据库
如何设置模型,以便字段 project_score
、employee_score
和 company_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您要问的是如下
或者,如果您的
Score
位于包含id
和name
列的数据库表中。您可以在其中创建与分数的关联
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-builders-in-rails.html。显示时,您还需要某种形式的辅助方法来显示相应的方法:
或者,如果您采用了
Score
表和belongs_to
关联方法< %= @something.project_score.name %>
I think what you're asking is as follows
Alternatively, if you had your
Score
s in a database table with anid
andname
column.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 ofscore_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:
Or if you have took the
Score
table andbelongs_to
association approach<%= @something.project_score.name %>
您可以将模型设置为返回如下所示的内容:
Then
<%= obj.select Model::FREQUENCY.each { |w| w } %>
在视图中。You can set-up you model to return something like this:
Then
<%= obj.select Model::FREQUENCY.each { |w| w } %>
in the View.