Note that there are some explanatory texts on larger screens.

plurals
  1. PORails, update method and validation
    primarykey
    data
    text
    <p>I have a problem and I need an idea how to fix my update method. I have an admin panel where I can create users. This form include name, mail, password, repeated password fields and it works fine. Then I want to have a list of all users and to edit these who I want. The problem is that I want to edit part of the information which is not included in the form of the registration and default is empty. In edit mode my form has two new fields - notes and absences. When I change these fields and call update method I see message that password and repeated password don't match which is validation in the registration but I do not have these files in edit mode. How could I fix this problem. This is part of my code:</p> <pre><code>class UsersController &lt; ApplicationController def edit @user = User.find(params[:id]) @title = "Edit user" end def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) flash[:success] = "Profile updated." redirect_to @user else @title = "Edit user" render 'edit' end end class User &lt; ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation validates :name, :presence =&gt; true, :length =&gt; { :maximum =&gt; 50 } validates :email, :presence =&gt; true email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, :presence =&gt; true, :format =&gt; { :with =&gt; email_regex }, :uniqueness =&gt; true validates :password, :presence =&gt; true, :confirmation =&gt; true, :length =&gt; { :within =&gt; 6..40 } before_save :encrypt_password def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end def self.authenticate(email, submitted_password) user = find_by_email(email) return nil if user.nil? return user if user.has_password?(submitted_password) end def self.authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user &amp;&amp; user.salt == cookie_salt) ? user : nil end private def encrypt_password self.salt = make_salt unless has_password?(password) self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload