对于那些真正拥有或正在观看 RailsCasts 的人,以及真正看过这两个视频的人: http : //railscasts.com/episodes/250-authentication-from-scratch?view=comments http://railscasts.com/episodes/250-authentication-from-scratch?view=comments //railscasts.com/episodes/250-authentication-from-scratch-revised
你会注意到,在他们两个中,在他所做的每次登录空白测试中,Ryan Bates 似乎并不关心在登录validation_presence 错误时,他会从/sessions/new 重定向到/sessions。这让我很困扰,我仍然很难找到解决方案?我有一种感觉,它相当简单,但我想它只是隐藏在某个可见的地方,但对我来说不是。
*这基本上是 RYAN BATES 代码: *他有错误 - 我有错误。:想法???
用户模型:
has_secure_password
attr_accessible :email, :password, :password_confirmation
validates_uniqueness_of :email
会话控制器:
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now.alert = "Email or password is invalid"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
会话登录页面:
<h1>Log In</h1>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</div>
<div class="actions"><%= submit_tag "Log In" %></div>
<% end %>