Guest User

Untitled

a guest
Apr 25th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. ## user controller
  2.  
  3. def edit_profile
  4. @user = session[:user]
  5. if request.post?
  6. @user.update_attributes(:first_name=>params[:user][:first_name]) if params[:user][:first_name] != @user.first_name
  7. @user.update_attributes(:last_name=>params[:user][:last_name]) if params[:user][:last_name] != @user.last_name
  8. @user.update_attributes(:email=>params[:user][:email]) if params[:user][:email] != @user.email
  9. if (!params[:user][:password].empty?)
  10. flash[:notice] = "Password must not have been empty"
  11. @user.update_attributes(:password=>params[:user][:password])
  12. @user.update_attributes(:password_confirmation => params[:user][:password_confirmation])
  13. end
  14. if @user.save
  15. flash[:notice] = "Profile has been saved"
  16. session[:user] = @user
  17. end
  18. end
  19. end
  20.  
  21. ## user view
  22. <%= error_messages_for 'user' %><br />
  23. <% form_tag :action=> 'edit_profile', :id => @user do %>
  24. <label for="user_first_name">First Name:</label><br />
  25. <%= text_field "user", "first_name", :size => 20 %><br />
  26. <label for="user_last_name">Last Name:</label><br />
  27. <%= text_field "user", "last_name", :size => 20 %><br />
  28. <label for="user_email">Email:</label><br />
  29. <%= text_field "user", "email", :size => 20 %><br />
  30. <label for="user_password">New password:</label><br />
  31. <%= password_field "user", "password", :size => 20, :value=>"" %><br />
  32. <label for="user_password_confirmation">Confirm new password:</label><br />
  33. <%= password_field "user", "password_confirmation", :size => 20, :value=>"" %><br />
  34. <%= submit_tag "Save Profile" %>
  35. <% end %>
  36. <% end %>
  37.  
  38. ## model
  39. attr_protected :id, :salt
  40. attr_accessor :password, :password_confirmation
  41.  
  42. validates_length_of :login, :within => 3..40
  43. validates_length_of :password, :within => 4..40
  44. validates_presence_of :login
  45. validates_presence_of :email
  46. validates_presence_of :first_name
  47. validates_presence_of :last_name
  48. validates_presence_of :password
  49. validates_presence_of :password_confirmation
  50. validates_presence_of :salt
  51. validates_uniqueness_of :login
  52. validates_uniqueness_of :email
  53. validates_confirmation_of :password
  54. validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"
  55.  
  56.  
  57. ## my question
  58. It should allow the password to be blank :( why isn't it working. Instead it's using validation to tell me that the password isn't allowed to be blank.
Add Comment
Please, Sign In to add comment