我刚开始使用这些东西学习ruby-on-rails: http://guides.rubyonrails.org/getting_started.html
我已经停止了这一步:
10安全性
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
respond_to do |format|
[...]
class CommentsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
def create
@post = Post.find(params[:post_id])
[...]
它不起作用。我的意思是,没有提示输入密码和登录,也没有错误。我刚刚那样做了,没有任何改变。
有人可以帮助我,或者至少指出我正确的方向来解决问题吗?
答案 0 :(得分:1)
这取决于您拥有的Rails版本。如果它是3.2它应该正常工作,你可以在页面顶部看到它说:
本指南基于Rails 3.2。此处显示的某些代码在早期版本的Rails中不起作用。
所以你可能想先检查一下,如果你的版本是较早版本,那么试试这个:
class ApplicationController < ActionController::Base
USER, PASSWORD = 'dhh', 'secret'
before_filter :authentication_check, :except => :index
#Any other method
private
def authentication_check
authenticate_or_request_with_http_basic do |user, password|
user == USER && password == PASSWORD
end
end
end
它应该正常工作。然后,最后一个选项,您可能已经提供了一次正确的凭据,现在应用程序会记住它是您并允许您执行所有操作,因此(只是为了确定)尝试更改其他任何内容的用户名。你想编辑这行,使它看起来像这样:
http_basic_authenticate_with :name => "anotherName", :password => "secret", :except => [:index, :show]
显然,这行代码(如前所述)仅适用于Rails 3.2(我实际上认为它也应该适用于3.1)或更多。