SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validation failing when updating attributes

    Hi guys,

    I'm pretty new to rails and I'm having a problem with my app failing validations on my user model when updating attributes.

    I've got a page where users can update various things about their profile. The problem is when I submit the form, I get errors about the password validation failing despite the password isn't being passed via the form.

    I'm just trying to update the country and DOB of a user.

    Any help would be great.
    hmm...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define password= like this:

    Code:
    class User < ActiveRecord::Base
      def password=(new_password)
         write_attribute :password, new_password unless new_password.blank?
       end
    end

  3. #3
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Thanks for the reply.

    My current password= method is as follows:

    Code Ruby:
      def password=(pass)
        @password=pass
        self.salt = User.random_string(10) if !self.salt?
        self.hashed_password = User.encrypt(@password, self.salt)
      end

    How could I edit that so it works?

    Thanks.
    hmm...

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, now I'm thinking about this again and I don't think this will fix it. Can you post the relevant view & controller code?

  5. #5
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    View (edit.rhtml)
    Code Ruby:
    <% form_for :user, :html => { :class => 'cmxform', :multipart => true } do |f| %>
        <%= error_messages_for 'user' %>
        <fieldset>
            <legend>Personal Details</legend>
            <ol>
                <li>
                    <label for="user_country">Country</label>
                  <%= f.country_select :country, ['United States', 'Canada', 'United Kingdom'] %>
              </li>
              <li>
                  <label for="user_birth_date">Date of Birth</label>
                  <%= f.date_select :birth_date, :order => [:day, :month, :year], :start_year => 1930 %>
                </li>
            <li>
                <label for="user_avatar">Display Image</label>
                <%= file_column_field 'user', 'avatar' %>
            </li>
                <li>
                    <%= submit_tag 'Update Profile', :class => 'button' %> 
            </li>
        </ol>
        </fieldset>
    <% end %>

    Controller
    Code Ruby:
      ...snip...
      def edit
          @user = User.find_by_id(session[:user_id])
          if request.post?
                if @user.update_attributes(params[:user])
                    flash[:message] = "Profile updated!"
                end
            end          
      end
    ...snip...

    Thanks.
    hmm...

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code looks good. Can you post the data in your db + your validations?

  7. #7
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Ruby:
    class User < ActiveRecord::Base
        acts_as_commentable
     
        has_many :images
     
      validates_length_of :login, :within => 3..40
      validates_length_of :password, :within => 5..40
      validates_presence_of :login, :email, :password, :password_confirmation, :salt
      validates_uniqueness_of :login, :email
      validates_confirmation_of :password
      validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"  
        validates_file_format_of :avatar, :in => ["gif", "png", "jpg"]     
    ...snip...
    hmm...

  8. #8
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CHeeSeBLiND View Post
    Hi, Thanks for the reply.

    My current password= method is as follows:

    Code Ruby:
      def password=(pass)
        @password=pass
        self.salt = User.random_string(10) if !self.salt?
        self.hashed_password = User.encrypt(@password, self.salt)
      end

    How could I edit that so it works?

    Thanks.
    AWDWR login system code?

  9. #9
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe the record in the db you're updating contains a non-valid password?

  10. #10
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skyblaze View Post
    AWDWR login system code?
    It was from some tutorial I found on a website. Alas, I can't remember which one it was haha. Still not read through AWDWR yet.

    Think I might give i a read actually.

    Quote Originally Posted by fenrir
    Maybe the record in the db you're updating contains a non-valid password?
    id login hashed_password email salt created_at country birth_date avatar
    2 test c172d663df50ddf85abeaffb576c88ca4b064a26 admin@test.com 1NVcvPoCUg 2007-07-02 16:40:29 United Kingdom 1984-07-13 6tVQBX6Q.jpg
    hmm...

  11. #11
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    validates_presence_of ... :password, ...
    but no password in the db. Do you see the problem?

  12. #12
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's the hashed password but not the actual password. Or the password_confirmation for that matter.

    How could I go about just updating the fields that are shown in the view posted above without the user having to input their password?
    hmm...

  13. #13
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    :if the validation:

    Code:
    validates_presence_of :password, :if => :new_password?
    
    def new_password?
      not (hashed_password and password.blank?) # made up by my subconsciousness, don't trust.
    end

  14. #14
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah brilliant.

    Thanks a lot
    hmm...

  15. #15
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm,

    I would suggest hashing the password in before_save filter of the model.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •