2013-05-16 2 views
0

작성 및 업데이트 작업 중에 다른 reject_ifs가 필요합니다.RAILS 3 (중첩 된 속성)의 동작에 따라 다른 reject_ifs

:

는 여기 첨부

has_many :attachments 
accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attributes| attributes['attachment_description'].blank? } 

내가 작업 및 업데이트 작업에 대한 또 다른 특정 옵션을 생성하기위한 특정 :reject_if 옵션을 원하는 모델 ... 예를 들어

에 대한 중첩 된 속성이

업데이트 중 ...

설명이 비어 있거나 nil 인 경우 거부합니다. 파일 이름이 비어 있거나 전무 경우 만들기에

:reject_if => proc { |attributes| attributes['attachment_description'].blank? } 

... 는 거부 :

:reject_if => proc { |attributes| attributes['attachment_file_name'].blank? } 

나는 accepts_nested_attributes_for 블록에 :on=> :update:on=> :create을 구현하는 방법도 모르겠습니다

대안이됩니다 감사하겠습니다. 감사.

편집 :

이 방법하지만 행운을 사용하려고 노력했다.

has_many :attachments, dependent: :destroy 
    accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attributes| attributes['attachment'].blank? }, on: :create 
    accepts_nested_attributes_for :attachments, :allow_destroy => true, on: :update 

그것은 말한다 ...

undefined method `key?' for nil:NilClass 

답변

0

귀하의 attributes 해시는 무기 호입니다, 그래서 당신은 그 'attachment' 키에 액세스 할 수 없습니다.

해시 값이 0이 아닌지 확인하고 그렇지 않으면 proc을 변경하여 nil case를 처리하십시오.

관련 문제