我目前正在学习 Rails 教程 4.0。我已经完成了 8.2.5 之前的所有内容。对于我的生活,我无法让用户能够登录。我的所有有效用户规范都失败了,当我在浏览器中尝试时,我总是收到用户名/密码组合失败消息。我检查了参数调试,并且我使用的电子邮件和密码与我在注册时设置的相匹配。用户存在于数据库中。我唯一能想到的就是remember_token的加密什么的。任何帮助都会很棒
工厂.rb
FactoryGirl.define do
factory :user do
name "James"
email "test@example.com"
password "foobar"
password_confirmation "foobar"
end
end
用户.rb
class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+).[a-z]+\z/i
validates :name , presence: true, length: { maximum: 50 }
validates :email , presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
before_save{ email.downcase! }
before_create :create_remember_token
has_secure_password
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
authentication_pages_specs
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "Signin Page" do
before { visit signin_path }
it { should have_content('Sign In') }
it { should have_title('Sign In') }
end
describe "Signing in" do
before { visit signin_path }
describe "invalid signin" do
before { click_button "Sign In" }
it { should have_title('Sign In') }
it { should have_selector('div.alert.alert-error',
text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_select('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button "Sign In"
end
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign Out', href: signout_path) }
it { should_not have_link('Sign In', href: signin_path) }
end
end
end
_header.html.erb
<header class='navbar navbar-fixed-top navbar-inverse'>
<div class='navbar-inner'>
<div class='container'>
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class='nav pull-right'>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id = "fat-menu" class = "dropdown">
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
Account<b class='caret'></b></a>
<ul class='dropdown-menu'>
<li><%= link_to 'Profile', current_user %></li>
<li><%= link_to 'Settings', '#' %></li>
<li class="divider"></li>
<li><%= link_to "Sign Out", signout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample Application!"
redirect_to @user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
session_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = "Invalid email/password combination"
render 'new'
end
end
def destroy
end
end
session_helper.rb
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
end