我正在关注:The infamous RoR blog tutorial security section
从某些奇怪的原因,当我试图到达特定的相关页面时,我得到了:
undefined method 'http_basic_authenticate_with' for PostsController:Class
错误
我正在使用:
知道可能导致这种情况的原因或者某个特定的宝石会丢失吗?
控制器代码的一部分:
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret",
:except => :index
# GET /posts
# GET /posts.xml
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
end
end
答案 0 :(得分:4)
我认为此方法现在只适用于Rails 3.1
,查看发行说明:
http://guides.rubyonrails.org/3_1_release_notes.html#action-controller
指南也说:
本指南基于Rails 3.1。这里显示的一些代码不会 在早期版本的Rails中工作。
答案 1 :(得分:4)
您可以尝试使用rails 3及更早版本:
class ApplicationController < ActionController::Base
USER, PASSWORD = 'dhh', 'secret'
before_filter :authentication_check, :except => :index
...
private
def authentication_check
authenticate_or_request_with_http_basic do |user, password|
user == USER && password == PASSWORD
end
end
end