Errors in calling private method

0 views
Skip to first unread message

Puneet Pandey

unread,
Dec 30, 2009, 3:08:04 PM12/30/09
to bangal...@googlegroups.com
Hi Folks,

I am getting a stranger error, sharing it with you, I need some help in that.

in one of my controller I have two methods, both will work like the same way.

def forgot
    user = User.find_by_email(params[:email])
    update_and_send_mail
end

def securityQA
    user = User.find(:first, :conditions => ["securityQuestion = ? AND securityAnswer = ?", params[:security_question], params[:security_answer]])
    update_and_send_mail
end

private
def update_and_send_mail
    if user
        user.new_random_password
        user.save
        Pony.mail(:to => user.email, :via => :smtp, :smtp => {
            :host     => 'MY-HOST-NAME',
            :port     => 'PORT-NUMBER',
            :user     => 'MY-USERNAME',
            :password => 'MY-PASSWORD',
            :auth     => :plain
        },
        :from => 'SENDER MAIL ID',
        :subject => 'PASSWORD',
        :body => user.password)
        render :update do |page|
            page.replace_html 'forgot_password_div', "We've sent you a mail on your mail Id #{user.email} with a new password."
            page.visual_effect :highlight, 'forgot_password_div'
        end
    else
        render :update do |page|
            page.replace_html 'forgot_password_div', "There are no records with this email Id."
            page.visual_effect :highlight, 'forgot_password_div'
        end
    end
end

When I am trying to run the script, I got the following error:

NameError (undefined local variable or method `user' for #<UsersController:0xb6e9d4bc>):
app/controllers/users_controller.rb:35:in `update_and_send_mail'
app/controllers/users_controller.rb:25:in `forgot'

You can refer bold sections for more details.

Why I am getting this error? Kindly help me on the same.


--
Cheers!!
Puneet Pandey
http://www.puneetpandey.com
http://www.puneetitengineer.wordpress.com

Puneet Pandey

unread,
Dec 30, 2009, 3:36:01 PM12/30/09
to bangal...@googlegroups.com
Heyy guys,

So here is the first update.. I tried to resolve the problem by doing this step:

update_and_send_mail(:user)

private
    def update_and_send_mail(user)
    end

Now I am getting second error:

NoMethodError (undefined method `new_random_password' for :user:Symbol):
app/controllers/users_controller.rb:36:in `update_and_send_mail'
app/controllers/users_controller.rb:25:in `forgot'

My new_random_password is in the User model.

Which has the following syntax:

def self.new_random_password
    self.password= Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--")[0,6]
    self.password_confirmation = self.password
end


Now the question is how would I pass new_random_password in private method?

Abhishek Parolkar

unread,
Dec 30, 2009, 9:35:25 PM12/30/09
to bangal...@googlegroups.com
You need to pass the ar_obj ... ie 'user' not symbol ':user' .

simply call it like update_and_send_mail(user)

Or .. make user as instance variable, @user .

-Abhishek


--

You received this message because you are subscribed to the Google Groups "BANGALORE RUG-Ruby Users Group" group.
To post to this group, send an email to bangal...@googlegroups.com.
To unsubscribe from this group, send email to bangalorerug...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bangalorerug?hl=en-GB.

Harish P

unread,
Dec 31, 2009, 1:50:10 AM12/31/09
to bangal...@googlegroups.com
Hi Puneet

Its a simple mistake, you are fetching the record from the database into a local variable (user), which is not accessible outside the method( i.e., in update_and_send_mail). So u can solve this by

1. passing the local variable as a parameter to update_and_send_mail

or

2. converting the local variable to instance variable(@user) which is accessible to all the methods within the class and making use of the instance variable in update_and_send_mail



Harish
--
Regards, Harish
Blogs: http://harish-in.blogspot.com
http://harishwords.wordpress.com

Bhavin Javia

unread,
Dec 31, 2009, 10:00:41 AM12/31/09
to bangal...@googlegroups.com
Hi Puneet,

Looks like you can benefit from reading the PickAxe book

free online version -> http://www.ruby-doc.org/docs/ProgrammingRuby/
or
buy the latest one -> http://pragprog.com/titles/ruby/programming-ruby

Cheers,
Bhavin
-------------------------------------------------
Bhavin Javia,
ThoughtWorks,
Bangalore, India.
+91 98454 64893

Puneet Pandey

unread,
Jan 1, 2010, 9:40:54 AM1/1/10
to bangal...@googlegroups.com
hey all,
 
first of all thanks for your quick response... I tried to follow above steps.. like this:
 
update_and_send_mail(user.email, user.password)
 
private
def update_and_send_mail(user, password)
end
 
It works fine for me.. but it is not saving the user, when I am writing user.save and giving me the error undefined local variable or method save. Please let me know how would I do that.. meanwhile I'll also try to apply few more strategies..

Harish P

unread,
Jan 1, 2010, 9:55:26 AM1/1/10
to bangal...@googlegroups.com
Hi puneeth,

It looks like you are prety confused with the way objects and active records are being used.

it was supposed to be

update_and_send_mail(user)

private
def update_and_send_mail(user)
    # code to save user and send email
end

Satish N Kota

unread,
Jan 1, 2010, 9:59:51 AM1/1/10
to bangal...@googlegroups.com

Hey Puneeth,

 

You are sending user.email in place of user and and trying to save it?  Please send the ar_obj…

 

update_and_send_mail(user.email, user.password)

 

private

def update_and_send_mail(user, password)

#here user=user.email? so

#user.save doesnot work…

end

 

Thanks and Regards

Satish N Kota

 


Bhavin Javia

unread,
Jan 2, 2010, 3:23:04 AM1/2/10
to bangal...@googlegroups.com
Hi Puneet,

Forgot to mention one more "must read" book for every RoR programmer - >
AWDwR (http://pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition)

Once, you had read this book from cover to cover, it will help you avoid such problems in future and save you a lot of time.

Cheers,
Bhavin
ThoughtWorks,
Bangalore, India.

You received this message because you are subscribed to the Google Groups "BANGALORE RUG-Ruby Users Group" group.
To post to this group, send an email to bangal...@googlegroups.com.
To unsubscribe from this group, send email to bangalorerug...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bangalorerug?hl=en-GB.




--
Cheers!!
Puneet Pandey
http://www.puneetpandey.com
http://www.puneetitengineer.wordpress.com

--

You received this message because you are subscribed to the Google Groups "BANGALORE RUG-Ruby Users Group" group.
To post to this group, send an email to bangal...@googlegroups.com.
To unsubscribe from this group, send email to bangalorerug...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bangalorerug?hl=en-GB.

--

You received this message because you are subscribed to the Google Groups "BANGALORE RUG-Ruby Users Group" group.
To post to this group, send an email to bangal...@googlegroups.com.
To unsubscribe from this group, send email to bangalorerug...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bangalorerug?hl=en-GB.
Reply all
Reply to author
Forward
0 new messages