您好我最近看过railscast#270(Rails 3.1中的身份验证)。我正在运行3.2 Rails atm。当我注册应用程序时,一切似乎都有用,但当我尝试使用完全相同的用户名和密码登录时,我被拒绝了。代码如下:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# username :string(255)
# password_digest :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
User.rb
class User < ActiveRecord::Base
attr_accessible :username, :password, :password_confirmation
# validations
has_secure_password
end
SessionsHelper.rb
module SessionsHelper
def signed_in?
!current_user.nil?
end
end
ApplicationHelper.rb
module ApplicationHelper
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_path, :flash => { :success => "Signed Up!" }
else
render :new
end
end
end
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_username(params[:username])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, :flash => { :success => "Signed In! " }
else
flash.now[:error] = 'Invalid email/password combination'
render :new
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, :flash => { :success => "Signed Out!" }
end
end
并且表单只是基本表单,其中包含错误消息部分,如果出现问题则显示flash消息。如果我没有提供任何代码或信息,请告诉我。在此先感谢:)
更新 - rails服务器日志(只有我能找到的日志)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)
Started GET "/assets/pages.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /pages.js - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /jquery.js - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /custom.css - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /jquery_ujs.js - 304 Not Modified (9ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /sessions.js - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /application.js - 304 Not Modified (4ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/bootstrap" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /bootstrap - 404 Not Found (4ms)
ActionController::RoutingError (No route matches [GET] "/assets/bootstrap"):
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /users.js - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/blueprint/print.css?body=1" for 127.0.0.1 at 2013-01-24 17:18:48 +1100
Served asset /blueprint/print.css - 304 Not Modified (0ms)
[2013-01-24 17:18:48] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true