Re: Noob question .each problem

radu puspana
2010-01-03T16:48:21Z

problem solved!!! thx to me :)

On Jan 3, 6:37 pm, radu puspana
<radupusp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> First, A Happy New Year to you all
> Second :
>
> this is my login action form the admin controller:
> def login
>     if request.post?
>       user = User.authenticate(params[:name], params[:password])
>       if user
>         if user.banned_status == "true"
>           flash.now[:notice] = "We are sorry.You can not log in beause
> you have been banned.Contact the admin at
r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org to solve this
> problem"
>
>         elsif user.blocked_status == "true"
>           flash.now[:notice] = "We are sorry.You are still
> blocked.Contact the admin at r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
to solve this problem"
>         else
>           session[:user_id] = user.id
>           redirect_to :action => "index"
>         end
>       else
>         flash.now[:notice] = "Invalid user/password combination #
> {user.id}"
>     end# 2nd if
>    end# 1st if
>   end#login
> ===============> this is my user model for the authenticate method :
>  def self.authenticate(name, password)
>         user = User.find(:all, :conditions => ["name =
:param",{:param
> => name}])
>         if user.size > 0
>             user.each do |correct_user|
>                 expected_password = User.encrypted_password(password,
> correct_user.salt)
>                 if correct_user.hashed_password == expected_password
>                     idx = user.index(correct_user)
>
>                    return user[idx]
>                 end
>             end#do
>             user = nil
>         else
>           user = nil
>         end#1st if
>     end#def
> =====> and the login view :
>
>     <% form_tag do %>
>
>
> Name:
>                 <%= text_field_tag :name, params[:name] %>
>
>
>
>                 <label
for="password">Password:
>                 <%= password_field_tag :password, params[:password]
%>
>
>
>
>                 <%= submit_tag "Login", :name => nil %>
>
>
>     <% end %>
>
>
> So the thing is that in my db i have two users with the same name, but
> no twith the same password.
>  id = 2
>            name = radu
>         surname = puspana
> hashed_password = 6b2711b337ae9b5c8a5932e60955f25dc42bcec1
>
>              id = 4
>            name = radu
>         surname = gica
> hashed_password = caf5488d972483c5b064a1afbe30b4370391ab73
>
> The problem is that when i try to login with the first name, radu and
> the password admin, it works fine, but if i try to login in with the
> second name(id =4) radu andthe pass gica1, it return the nil value
> back to the controller, so user in the controller will be nil, showing
> the message "Invalid username/password" although the name and
pass are
> correct...
>
> what the heck???
>
> a billion thx in advance,
> radu