For some reason my login controller refuses to access a method i have
defined for another class. This is remedial stuff but my searches
haven''t cleared up the issue. It is creating the model but
can''t access
the method. The method is NOT protected or private. Any thoughts? The
error:
undefined method `try_to_login'' for #<Person:0x37ed3f0>
LOGIN CONTROLLER:
class LoginController < ApplicationController
def login
#if a regular page load
if request.get?
#destroy session info and create new person object
session[:person_id] = nil
@person = Person.new
else
#create a new user object and fill it with entered form data
@person = Person.new(params[:person])
#take user object and ''try to log in'' (see person model)
logged_in_person = @person.try_to_login
#if log in is successful
if logged_in_person
#set session id to user id and send home
session[:id] = logged_in_person.id
redirect_to :controller => ''room'',
:action => ''list''
#otherwise complain
else
flash[:notice] = "Invalid email or password"
end
end
end
PERSON MODEL:
class Person < ActiveRecord::Base
####################################
#LOGIN METHODS (see login controller)
def self.login(name, password)
#hash plaintext password entry
hashed_password = hash_password(password || "")
#return first user record matching entered name and converted
password
find( :first,
:conditions => ["name = ? and hashed_password = ?",
name, hashed_password])
end
def self.try_to_login
Person.login(self.name, self.password)
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Hello Taylor,> For some reason my login controller refuses to access a method > i have defined for another class. This is remedial stuff but my > searches haven't cleared up the issue. It is creating the model > but can't access the method. The method is NOT protected or > private. Any thoughts? The error: > > undefined method `try_to_login' for #<Person:0x37ed3f0>[...]> #take user object and 'try to log in' (see person model) > logged_in_person = @person.try_to_loginYou're calling #try_to_login as an instance method of @person instance. [...]> class Person < ActiveRecord::Base[...]> def self.try_to_login > Person.login(self.name, self.password) > endYou've defined #try_to_login as a class method. Furthermore inside the #try_to_login code, self.name and self.password will be calls to class methods Person#name and Person#password and not instance methods. So maybe you have to change #try_to_login to be an instance method. -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
My understanding was that all class instances inherit all class methods. ''Programming Ruby, 2nd Edition'' seems to validate this. Am I wrong? How can I define an ''instance method'' separate from the class? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-29 15:54 UTC
Re: Undefined method IS defined in login subsystem
Hi -- On Sun, 29 Oct 2006, Taylor Strait wrote:> > My understanding was that all class instances inherit all class methods. > ''Programming Ruby, 2nd Edition'' seems to validate this. Am I wrong? > How can I define an ''instance method'' separate from the class? Thanks!class C def m end end m is an instance method. I''m not sure whether that addresses your question, though. Can you expand a bit on the problem? David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
David, I have your book! It is a great intro to ruby and it taught me a lot! But obviously, not enough :( Back to the question: try_to_login is clearly a public instance method of Person. @person is clearly an instance of Person. So why does @person.try_to_login fail with ''undefined method `try_to_login'' for #<Person:0x37ed3f0>?'' -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-29 18:13 UTC
Re: Undefined method IS defined in login subsystem
Hi -- On Sun, 29 Oct 2006, Taylor Strait wrote:> > David, > > I have your book! It is a great intro to ruby and it taught me a lot!Thanks.> But obviously, not enough :(Whoops :-)>Back to the question: > > try_to_login is clearly a public instance method of Person. @person is > clearly an instance of Person. So why does @person.try_to_login fail > with ''undefined method `try_to_login'' for #<Person:0x37ed3f0>?''You''ll need to show some code. There''s something going on that''s impossible to pinpoint otherwise. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
David, I can assure you that my lack of proficiency has nothing to do
with your book and everything to do with my brain! Here is everything:
APPLICATION.RHTML
<!--LOGIN / USER CP-->
<div id="login">
<% if session[:id] %>
<%= render :partial => ''login/logged_in'' %>
<% else %>
<%= render :partial => ''login/_login'' %>
<% end %>
</div>
_LOGIN.RHTML
<%= form_tag(:controller => ''Login'', :action =>
''login'') %>
<p>
<label for="person_email">Email</label>
<%= text_field ''person'', ''email'' %>
</p>
<p>
<label for="person_password">Password</label>
<%= password_field ''person'', ''password''
%>
</p>
<%= submit_tag "Login" %>
LOGIN CONTROLLER
class LoginController < ApplicationController
def login
#if a regular page load
if request.get?
#destroy session info and create new person object
session[:person_id] = nil
@person = Person.new
else
#create a new user object and fill it with entered form data
@person = Person.new(params[:person])
#take user object and ''try to log in'' (see person model)
logged_in_person = @person.try_to_login
#if log in is successful
if logged_in_person
#set session id to user id and send home
session[:id] = logged_in_person.id
redirect_to :controller => ''room'',
:action => ''list''
#otherwise complain
else
flash[:notice] = "Invalid email or password"
end
end
end
PERSON MODEL
class Person < ActiveRecord::Base
####################################
#LOGIN METHODS (see login controller)
def self.login(name, password)
#hash plaintext password entry
hashed_password = hash_password(password || "")
#return first user record matching entered name and password
find( :first,
:conditions => ["name = ? and hashed_password = ?",
name, hashed_password])
end
def self.try_to_login
Person.login(self.name, self.password)
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Hello Taylor,> try_to_login is clearly a public instance method of Person.No it isn't. See your code : class Person < ActiveRecord::Base # ... def self.try_to_login Person.login(self.name, self.password) end end> @person is clearly an instance of Person.Yes.>So why does @person.try_to_login fail with 'undefined method > `try_to_login' for #<Person:0x37ed3f0>?'Because as I said earlier, you defined try_to_login as a class method. If you don't believe me, read again the chapter about instance methods/class methods from David's book :) -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 30/10/2006, at 4:10 AM, Taylor Strait wrote:> try_to_login is clearly a public instance method of Person. > @person is > clearly an instance of Person. So why does @person.try_to_login fail > with ''undefined method `try_to_login'' for #<Person:0x37ed3f0>?''No, try_to_login is a public class method of Person. This is a class method: class Person def self.blah ... end end which you would call as Person.blah. This is an instance method: class Person def blah ... end end which you would call as: person = Person.new person.blah You''re trying to call the try_to_login class method that you''ve defined on an instance of Person, which doesn''t work. Pete Yandell http://aliencamel.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks this was all very helpful. Removing self. from self.try_to_login fixed everything. And I learned about Class vs. instance variables. Gracias! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---