Keep your controllers clean and forms flexible by adding virtual attributes to your model. This very powerful technique allows you to create form fields which may not directly relate to the database.
<!-- users/new.rhtml -->
<%= f.text_field :full_name %>
# models/user.rb
def full_name
[first_name, last_name].join(' ')
end
def full_name=(name)
split = name.split(' ', 2)
self.first_name = split.first
self.last_name = split.last
end
本文介绍了一种通过为模型添加虚拟属性来保持控制器清洁和表单灵活的技术。该技术允许创建与数据库不直接关联的表单字段,例如将全名拆分为名和姓。

被折叠的 条评论
为什么被折叠?



