Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## View:
- <%= text_field 'user', 'username', { :autocomplete => "off" } %>
- <%= observe_field "user_username",
- :url => account_url(:action => "validate"),
- :with => "user[username]",
- :frequency => 0.25 %>
- <%= content_tag "div", "", :id => "error_username", :style => "display:none;" %>
- ## Controller:
- def validate
- if request.xhr?
- @user = User.new( params[:user] )
- # Try to validate the user (this will add errors)
- @user.valid?
- render :action => "validate.rjs"
- else
- render :nothing => true
- end
- end
- ## validate.rjs
- attrs = params[:user].map {|k,v| k }
- attrs.each do |attr|
- if @user.errors.on(attr)
- errors = @user.errors.on(attr)
- page["error_#{attr}"].replace_html @user.class.human_attribute_name(attr) + " " + ( errors.is_a?(Array) ? errors.first : errors )
- page << "if ($('error_#{attr}').style.display == 'none') {"
- page.visual_effect :blind_down, "error_#{attr}", :duration => 0.25
- page << "}"
- else
- page << "if ($('error_#{attr}').style.display != 'none') {"
- page.visual_effect :blind_up, "error_#{attr}", :duration => 0.25
- page << "}"
- end
- end
Add Comment
Please, Sign In to add comment