敏捷开发时login遇到的问题

敏捷开发时login遇到的问题

Mysql::Error: #42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''as' and hashed_password = 'as') LIMIT 1' at line 1: SELECT * FROM users WHERE (name 'as' and hashed_password = 'as') LIMIT 1


我按照他的login做的页面

user
class php?name=User" onclick="tagshow(event)" class="t_tag">User < ActiveRecord::Base
 def self.login(name,hashed_password)
#hashed_password = User.hashed_password(hashed_password || "" )
find(:first,
 :conditions =>["name ? and hashed_password = ?",
       name, hashed_password])
       end
def try_to_login
User.login(self.name,self.hashed_password)
end
end


login.rhtml
<% @page_title="ADD a User"%>
<%= error_messages_for 'user'%>
<%=form_tag %>
<table>
<tr>
<td>User name:</td>
<td><%= text_field("user","name")%></td>
</tr>
<tr>
<td>Password:</td>
<td><%= password_field("user","hashed_password")%></td>
</tr>

    <td></td>
    <td><input type="submit" value="login" /></td>
    </tr>
    </table>
    <%=end_form_tag%>


login

 def login
  if request.get?
  session[:user_id] = nil
  @user = User.new
  else
  @user =User.new(params[:user])
  logged_in_user =@user.try_to_login
  if logged_in_user
   session[:user_id]= logged_ni+user.id
   reditect_to(:action => "index")
  else
   flash[:notice]="INcalid user"
  end
  end
 end
user
class User < ActiveRecord::Base
 def self.login(name,hashed_password)
#hashed_password = User.hashed_password(hashed_password || "" )
find(:first,
 :conditions =>["name = ? and hashed_password = ?",
       name, hashed_password])
       end
def try_to_login
User.login(self.name,self.hashed_password)
end
end
谢谢楼上的指出问题,但是发现还是有问题

当我把add_user

中的hashed_password改称password后会出现找不到password方法。因此我在login.rhtml里面都是用的hashed_password.

user.rb

class User < ActiveRecord::Base

 def self.login(name,hashed_password)
hashed_password = hash_password(hashed_password || "" )
find(:first,
 :conditions =>["name = ? and hashed_password = ?",
       name, hashed_password])
 end
def try_to_login
User.login(self.name, self.hashed_password)
end
end


login.rhtml

<% @page_title="ADD a User"%>
<%= error_messages_for 'user'%>
<%=form_tag %>
<table>
<tr>
<td>User name:</td>
<td><%= text_field("user","name")%></td>
</tr>
<tr>
<td>Password:</td>
<td><%= password_field("user","hashed_password")%></td>
</tr>

    <td></td>
    <td><input type="submit" value="login" /></td>
    </tr>
    </table>
    <%=end_form_tag%>
错误提示是

undefined method `hash_password' for User:Class
当我注掉
user。rb 中的hashed_password = hash_password(hashed_password || "" )
这一行之后

我在login的时候,无错误提示,页面显示在login不动了!
定义hash_password 应该是static的吧。

[Copy to clipboard] [ - ]
问题已经解决,谢谢楼上的!