2012-02-21 2 views
1

나는 simple_form에 사용하기 위해, 가상 속성을 가진 모델을 가지고 :Inherited_resources 사용자 지정 오류

class Sms < ActiveRecord::Base 
attr_accessor :delayed_send, :send_time_date, :send_time_time 

I 양식을위한/SMS에/새 :

= simple_form_for([:admin, resource]) do |f| 
    ... 
    .clear 
    .field.grid_3 
    = f.input :delayed_send, :as => :boolean, :label => "Отложенная отправка на:" 
    .clear 
    .field.grid_3 
    = f.input :send_time_date, :as => :string, :input_html => { :class => 'date_picker' }, :disabled => true, :label => "Дату:" 
    .clear 
    .field.grid_1 
    = f.input :send_time_time, :as => :string, :disabled => true, :label => "Время:", :input_html => { :value => (Time.now + 1.minute).strftime("%H:%M") } 
    .clear 
    .actions.grid_3 
    = f.submit "Отправить" 

그리고 내가 싶어 안에 모든 가상 속성을 확인 내 SmsesController, 작업 만들기, 그리고 그것이 유효하지 않은 경우 - 오류를 보여줍니다. 그러나 그것은 작동하지 않습니다

class Admin::SmsesController < Admin::InheritedResources 
def create 
    @sms.errors.add(:send_time, "Incorrect") if composed_send_time_invalid? 
    super 
end 
내가 inherited_resources를 사용하는 경우

어떻게 내가, 내 사용자 지정 오류를 추가해야합니까? 당신이 컨트롤러의 유효성을 검사하고 특정 이유가 아니라면

+0

컨트롤러가 아닌 컨트롤러에서 유효성 검사를 수행해야하는 특별한 이유가 있습니까? – miked

답변

1

, 검증은 모델에 있어야합니다 : 컨트롤러에서

class Sms < ActiveRecord::Base 

    #two ways you can validate: 
    #1.use a custom validation routine 
    validate :my_validation 

    def my_validation 
    errors.add(:send_time, "Incorrect") if composed_send_time_invalid? 
    end 

    #OR 2. validate the attribute with the condition tested in a proc. 
    validates :send_time, :message=>"Incorrect", :if=>Proc.new{|s| s.composed_send_time_invalid?} 
end 

, 슈팅을 막아 트리거 (또는 object.valid을 위해 전화?) 이 유효성 검사가 실행됩니다. 그런 다음 컨트롤러에서 응답을 처리하여 필요한 경우 조치를 다시 렌더링 할 수 있습니다.

+0

고마워. 모든 문제를 해결하기 위해 유효성 검사를 모델로 이동하십시오. – BazZy