[英]Rails: How do I save a string representation back into a model?
I have an Employee
model that has a SecurityClearanceLevel
. 我有一个具有SecurityClearanceLevel
的Employee
模型。 When you make a new employee, you're asked to set a SecurityClearanceLevel
by choosing from a <select>
list. 当您结交新员工时,系统会要求您从<select>
列表中进行选择来设置SecurityClearanceLevel
。 The problem is that when I save the object, it's a string, not a SecurityClearanceLevel
, so the save fails. 问题是,当我保存对象时,它是一个字符串,而不是SecurityClearanceLevel
,因此保存失败。
Where do I take care of this kind of back-and-forth conversion, and what's the best way to do it? 我该在哪里进行这种来回转换,最好的方法是什么?
Rails gives you a few variations around doing this. Rails为您提供了一些执行此操作的变体。 You should be able to do something like this: 您应该能够执行以下操作:
<%= collection_select(:employee, :security_clearance_level_id,
SecurityClearanceLevel.all, :id, :name) %>
Take a look at the Rails Guide on the select and option tags for the full details. 查看select和option标签上的Rails Guide,以获取全部详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.