Working through the tutorials in the RailsSpace book, and cannot
figure out what I am doing wrong here. Full code for the edit function
which is tossing the error as well as appropriate code from the model
the actual error are pasted below.
Thank you for your help.
Dave
NoMethodError in UserController#edit
private method `correct_password?’ called for #User:0x485ee28
RAILS_ROOT: C:/instant_rails/InstantRails/rails_apps/puget/config/…
Application Trace | Framework Trace | Full Trace
C:/instant_rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib/active_record/base.rb:1857:in method_missing' #{RAILS_ROOT}/app/controllers/user_controller.rb:63:in
edit’
Request
Parameters: {“user”=>{“password_confirmation”=>“blip”,
“current_password”=>“blippi”, “password”=>“bloppi”},
“commit”=>“Update”, “attribute”=>“password”}
Show session dump
:user_id: 6
flash: !map:ActionController::Flash::FlashHash {}
Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}
user_controller.rb
#Edit the user’s basic info.
def edit
@title = “Edit your User Information”
@user = User.find(session[:user_id])
if param_posted?(:user)
attribute = params[:attribute]
case attribute
when “email”
try_to_update @user, attribute
when “password”
if @user.correct_password?(params)
try_to_update @user, attribute
else
@user.password_errors(params)
end
end
end
#For security purposes, never fill in the password fields.
@user.clear_password
end
Try to update the user, redirecting if successful.
def try_to_update(user, attribute)
if user.update_attributes(params[:user])
flash[:notice] = “User #{attribute} updated.”
redirect_to :action => “index”
end
end
User.rb
#Return true if the password from params is correct.
def correct_password?(params)
current_password = params[:user] [:current_password]
password == current_password
end
#Generate message for password errors.
def password_errors(params)
#Use User models valid? method to generate error message
#for a password mismatch (if any)
self.password = params[:user][:password]
self.password_confirmation = params[:user]
[:password_confirmation]
valid?
#The current password is incorrect, so add an error message.
errors.add(:current_password, “is incorrect”)
end