Railsでリアルタイムフォームバリデーションする
UIを作るときリアルタイムでフォームのエラーチェックをすることは多いよ。
そんなときは以下のようなコードを使うと上手く行くかも。
#model def validate_for_ajax n,v if attribute_names.find{|i| i == n} @attributes[n] = v valid? errors.on(n) ? "ng" : "ok" else nil end end #controller def valdiate_attribute @model = Model.find __some_find_info__ render :text => @model.validate_for_ajax(params[:attribute], params[:value]) end #view observe_field form_id, :url => {:action => :valdiate_attribute}, :with => "'attribute=ATTRIBUTE}&value=' + value", :complete => "$('EDIT_ID').className = request.responseText;"
これでフォームの値によってedit_idのクラスをokとngを切り替えることができるよ。