我是 Rails 和 Mongoid 的新手。我正在尝试创建一个登录页面。问题是如果出现用户名或密码错误,我无法发出错误消息。下面是我的代码。
模型用户.rb
def self.authenticate(login, pass)
if user = first(:conditions => {:username => login})
return user if user.password==(pass)
end
return false
end
控制器 user_auth.rb
def login
if request.post? && @user = User.authenticate(params[:user][:username],params[:user][:password])
redirect_to(:controller => 'posts')
else
@user = User.new()
end
end
查看 login.html.erb
<% form_for(@user,:url => { :action => "login" } ) do |d| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<%= pluralize(@user.errors.count, "error") %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p> Your Name: <%= d.text_field :username %><br /> </p>
<p> Password: <%= d.password_field :password %><br/></p>
<p><%=d.submit "Login" %></p>
<% end %>