フォームヘルパーの仮想属性で奇妙なエラーが発生しています。
私のモデルは次のようになります。
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
助言がありますか?