I''ve got a User model, which holds the following (excerpt): def try_to_authenticate User.authenticate(self.username, self.password) end .. private def self.hash_password(password) Digest::SHA1.hexdigest(password) end def self.authenticate(username, password) @user = User.find(:all, :conditions => ["username = ? AND password = ?", params[:username], self.hash_password(params[:password])) if @user.blank? raise "Incorrect username or password" end # Return the user object we found @user end However, when called from my LoginController, like so: def login if request.get? session[:user_id] = nil @user = User.new else @user = User.new(params[:user]) # TODO: wrap this in a rescue block to handle exception authenticated_user = @user.try_to_authenticate if authenticated_user session[:user_id] = authenticated_user.id redirect_to :action => session[:intended_action], :controller => session[:intended_controller] else flash[:notice] = ''Invalid username or password.'' end end end I get an error on the User.find(..) line that there is no such thing as params[]. I had it working but then I edited/moved the code around, so can anybody suggest why it now will not search for the user correctly? Is there a better way to do it? Cheers. -- Posted via http://www.ruby-forum.com/.
the class method doesn''t have access to the params array from the controller. just use the passed in method argument names ''username'' and ''password'' On 3/13/06, David <null@example.com> wrote:> > I''ve got a User model, which holds the following (excerpt): > > def try_to_authenticate > User.authenticate(self.username, self.password) > end > > .. > > private > def self.hash_password(password) > Digest::SHA1.hexdigest(password) > end > > def self.authenticate(username, password) > @user = User.find(:all, :conditions => ["username = ? AND > password = ?", > params[:username], > self.hash_password(params[:password])) > > if @user.blank? > raise "Incorrect username or password" > end > # Return the user object we found > @user > end > > However, when called from my LoginController, like so: > > def login > if request.get? > session[:user_id] = nil > @user = User.new > else > @user = User.new(params[:user]) > # TODO: wrap this in a rescue block to handle exception > authenticated_user = @user.try_to_authenticate > if authenticated_user > session[:user_id] = authenticated_user.id > redirect_to :action => session[:intended_action], :controller => > session[:intended_controller] > else > flash[:notice] = ''Invalid username or password.'' > end > end > end > > I get an error on the User.find(..) line that there is no such thing as > params[]. I had it working but then I edited/moved the code around, so > can anybody suggest why it now will not search for the user correctly? > > Is there a better way to do it? > > Cheers. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/20345aa2/attachment.html
Chris Hall wrote:> the class method doesn''t have access to the params array from the > controller. > > just use the passed in method argument names ''username'' and ''password''Thanks Chris, this is perfect. I hadn''t realised that params[] was for the controller, not the method. Time to read up more on scope and such in Ruby :) David -- Posted via http://www.ruby-forum.com/.
Hello, I am trying to create a new record in a database by modifying the create method generated by scaffold. I have a situation where i want to add new student and while creating a new student, i am able to choose the department the student belongs to. However when i say create, i get an error saying that Department expected, got string. *Parameters*: {"commit"=>"Create", "student"=>{"department"=>"1"} the method def create @student = Student.new(params[:student]) #error caused at this line if @student.save :<snip> end The department field is of type integer so it looks like it expects an integer value while department returns a string. How do i convert it to an integer so that i can succesfully add a new student to the database thanks Naveen -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060322/41adbb54/attachment.html
Hi, I think even if you use integer you will still get similar error, except it will say Fixnum instead of string. Because it seems like :department should be of object Department, not the foreign key. Cheers, Andy On 3/22/06, Preethi Naveen <preethi.naveen@gmail.com> wrote:> > Hello, > > I am trying to create a new record in a database by modifying the create > method generated by scaffold. I have a situation where i want to add new > student and while creating a new student, i am able to choose the department > the student belongs to. However when i say create, i get an error saying > that Department expected, got string. > > Parameters: {"commit"=>"Create", "student"=>{"department"=>"1"} > > the method > > def create > @student = Student.new(params[:student]) #error caused at this line > if @student.save > :<snip> > end > > The department field is of type integer > so it looks like it expects an integer value while department returns a > string. How do i convert it to an integer so that i can succesfully add a > new student to the database > > thanks > > Naveen > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Hi, In your model you could explicitly state the relationship class Student < ActionRecord::Base belongs_to :department, :class_name="department", :foriegn_key => ''department_id'' end Then in the veiw use <%= text_field "student", "department_id" %> or similar for a select box. I had similar problems when I was trying to get a form working and found that by doing this it worked. I think there is supposed to be some rails magic that does this for you but I don''t seem to have much luck with it. Cheers On 3/22/06, Andy Shen <andy.s.shen@gmail.com> wrote:> > Hi, > > I think even if you use integer you will still get similar error, > except it will say Fixnum instead of string. > Because it seems like :department should be of object Department, not > the foreign key. > > Cheers, > Andy > > On 3/22/06, Preethi Naveen <preethi.naveen@gmail.com> wrote: > > > > Hello, > > > > I am trying to create a new record in a database by modifying the create > > method generated by scaffold. I have a situation where i want to add new > > student and while creating a new student, i am able to choose the > department > > the student belongs to. However when i say create, i get an error saying > > that Department expected, got string. > > > > Parameters: {"commit"=>"Create", "student"=>{"department"=>"1"} > > > > the method > > > > def create > > @student = Student.new(params[:student]) #error caused at this line > > if @student.save > > :<snip> > > end > > > > The department field is of type integer > > so it looks like it expects an integer value while department returns a > > string. How do i convert it to an integer so that i can succesfully add > a > > new student to the database > > > > thanks > > > > Naveen > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060322/8118fbde/attachment-0001.html
is the foreign key department_id a field in the main table student? On 3/22/06, Liquid <has.sox@gmail.com> wrote:> > Hi, > > In your model you could explicitly state the relationship > > class Student < ActionRecord::Base > belongs_to :department, :class_name="department", :foriegn_key => > ''department_id'' > end > > Then in the veiw use > <%= text_field "student", "department_id" %> > > or similar for a select box. > > I had similar problems when I was trying to get a form working and found > that by doing this it worked. I think there is supposed to be some rails > magic that does this for you but I don''t seem to have much luck with it. > > Cheers > > > On 3/22/06, Andy Shen <andy.s.shen@gmail.com> wrote: > > > > Hi, > > > > I think even if you use integer you will still get similar error, > > except it will say Fixnum instead of string. > > Because it seems like :department should be of object Department, not > > the foreign key. > > > > Cheers, > > Andy > > > > On 3/22/06, Preethi Naveen <preethi.naveen@gmail.com> wrote: > > > > > > Hello, > > > > > > I am trying to create a new record in a database by modifying the > > create > > > method generated by scaffold. I have a situation where i want to add > > new > > > student and while creating a new student, i am able to choose the > > department > > > the student belongs to. However when i say create, i get an error > > saying > > > that Department expected, got string. > > > > > > Parameters: {"commit"=>"Create", "student"=>{"department"=>"1"} > > > > > > the method > > > > > > def create > > > @student = Student.new(params[:student]) #error caused at this > > line > > > if @student.save > > > :<snip> > > > end > > > > > > The department field is of type integer > > > so it looks like it expects an integer value while department returns > > a > > > string. How do i convert it to an integer so that i can succesfully > > add a > > > new student to the database > > > > > > thanks > > > > > > Naveen > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Dattatraya ------------------------------------------------- Work fascinates me, I can stare at it for hours...!! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060322/c76a84be/attachment-0001.html
Yes... It follows the belong_to requirements. But when you state is specifically the field name could be any name of type int. On 3/22/06, Dattatraya Gokhale <dgokhale@gmail.com> wrote:> > is the foreign key department_id a field in the main table student? > > > On 3/22/06, Liquid <has.sox@gmail.com> wrote: > > > > Hi, > > > > In your model you could explicitly state the relationship > > > > class Student < ActionRecord::Base > > belongs_to :department, :class_name="department", :foriegn_key => > > ''department_id'' > > end > > > > Then in the veiw use > > <%= text_field "student", "department_id" %> > > > > or similar for a select box. > > > > I had similar problems when I was trying to get a form working and found > > that by doing this it worked. I think there is supposed to be some rails > > magic that does this for you but I don''t seem to have much luck with it. > > > > Cheers > > > > > > On 3/22/06, Andy Shen < andy.s.shen@gmail.com> wrote: > > > > > > Hi, > > > > > > I think even if you use integer you will still get similar error, > > > except it will say Fixnum instead of string. > > > Because it seems like :department should be of object Department, not > > > the foreign key. > > > > > > Cheers, > > > Andy > > > > > > On 3/22/06, Preethi Naveen <preethi.naveen@gmail.com> wrote: > > > > > > > > Hello, > > > > > > > > I am trying to create a new record in a database by modifying the > > > create > > > > method generated by scaffold. I have a situation where i want to add > > > new > > > > student and while creating a new student, i am able to choose the > > > department > > > > the student belongs to. However when i say create, i get an error > > > saying > > > > that Department expected, got string. > > > > > > > > Parameters: {"commit"=>"Create", "student"=>{"department"=>"1"} > > > > > > > > the method > > > > > > > > def create > > > > @student = Student.new(params[:student]) #error caused at this > > > line > > > > if @student.save > > > > :<snip> > > > > end > > > > > > > > The department field is of type integer > > > > so it looks like it expects an integer value while department > > > returns a > > > > string. How do i convert it to an integer so that i can succesfully > > > add a > > > > new student to the database > > > > > > > > thanks > > > > > > > > Naveen > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Dattatraya > > ------------------------------------------------- > Work fascinates me, I can stare at it for hours...!! > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060322/89681d73/attachment.html