我在表单助手中的虚拟属性有一个奇怪的错误。
我的模型如下所示:
class Folder < ActiveRecord::Base
...
# VIRTUAL ATTRIBUTES
def parent_name
self.parent.name
end
def parent_name=(name)
self.parent = self.class.find_by_name(name)
end
...
end
我正在使用 HAML 和 SimpleForm。当我像这样使用我的表格时......
= simple_form_for [@collection, form], :html => { :class => 'full' } do |f|
= f.input :name
= f.input :description
= f.submit
...它完美地工作。但是,如果我尝试像这样访问虚拟属性...
= simple_form_for [@collection, form], :html => { :class => 'full' } do |f|
= f.input :name
= f.input :parent_name
= f.input :description
= f.submit
...我收到此错误:
NoMethodError in Folders#index
Showing ... where line #3 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #3):
1: = simple_form_for [@collection, form], :html => { :class => 'full' } do |f|
2: = f.input :name
3: = f.input :parent_name
4: = f.input :description
5: = f.submit
有什么建议么?