Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. ## View:
  2.  
  3. <%= text_field 'user', 'username', { :autocomplete => "off" } %>
  4. <%= observe_field "user_username",
  5. :url => account_url(:action => "validate"),
  6. :with => "user[username]",
  7. :frequency => 0.25 %>
  8. <%= content_tag "div", "", :id => "error_username", :style => "display:none;" %>
  9.  
  10. ## Controller:
  11.  
  12. def validate
  13. if request.xhr?
  14. @user = User.new( params[:user] )
  15. # Try to validate the user (this will add errors)
  16. @user.valid?
  17. render :action => "validate.rjs"
  18. else
  19. render :nothing => true
  20. end
  21. end
  22.  
  23. ## validate.rjs
  24.  
  25. attrs = params[:user].map {|k,v| k }
  26.  
  27. attrs.each do |attr|
  28. if @user.errors.on(attr)
  29. errors = @user.errors.on(attr)
  30. page["error_#{attr}"].replace_html @user.class.human_attribute_name(attr) + " " + ( errors.is_a?(Array) ? errors.first : errors )
  31.  
  32. page << "if ($('error_#{attr}').style.display == 'none') {"
  33. page.visual_effect :blind_down, "error_#{attr}", :duration => 0.25
  34. page << "}"
  35.  
  36. else
  37. page << "if ($('error_#{attr}').style.display != 'none') {"
  38. page.visual_effect :blind_up, "error_#{attr}", :duration => 0.25
  39. page << "}"
  40. end
  41. end
Add Comment
Please, Sign In to add comment