Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR: undefined method `http_basic_authenticate_with'

I'm following: The infamous RoR blog tutorial security section

And from some odd reason when trying to reach the specific relevant page i'm getting a:

undefined method 'http_basic_authenticate_with' for PostsController:Class error

I'm using:

  • Rails 3.0.9,
  • RubyGems 1.8.11,
  • Ruby 1.9.2p290

Any idea what might cause this or maybe a specific gem which is missing?

part of the controller code:

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
like image 336
Mikey S. Avatar asked Oct 14 '25 13:10

Mikey S.


2 Answers

I think this method is only available to Rails 3.1 now, looking at the release notes:

http://guides.rubyonrails.org/3_1_release_notes.html#action-controller

Also the guide says:

This Guide is based on Rails 3.1. Some of the code shown here will not work in earlier versions of Rails.

enter image description here

like image 167
Mr_Nizzle Avatar answered Oct 17 '25 01:10

Mr_Nizzle


You can try this for rails version 3 and earlier:

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
like image 33
Abhishek Bahuguna Avatar answered Oct 17 '25 02:10

Abhishek Bahuguna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!