small medium large xlarge

Generic-user-small
12 Oct 2011, 07:40
Rob (2 posts)

I’m working through the Playtime section of Chapter 14 and I’m a little stuck on the whole authenticate_or_request_with_http_basic bit

In my ApplicationController the authorize method looks like this:

def authorize
    if request.format == Mime::HTML
      unless User.find_by_id(session[:user_id]) || User.count.zero?
        redirect_to login_url, notice: "Please log in"
      end
    else
      authenticate_or_request_with_http_basic('Depot') do |username, password|
        username == 'dave' && password == 'secret'
        user = User.find_by_name('dave')
        session[:user_id] = user.id
      end
    end
  end

When I use curl to request the atom feed:

curl --silent --user dave:secret http://localhost:3000/products/2/who_bought.xml
Started GET "/products/2/who_bought.xml" for 127.0.0.1 at 2011-10-12 00:28:19 -0700
  Processing by ProductsController#who_bought as XML
  Parameters: {"id"=>"2"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."name" = 'dave' LIMIT 1
  Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", "2"]]
Completed 406 Not Acceptable in 12ms

I’m not sure if I’ve set things up properly in the controller or why it would be returning a 406 Not Acceptable. Do I need to set a format somewhere…?

Samr_small_pragsmall
12 Oct 2011, 11:04
Sam Ruby (634 posts)

406 is indeed related to the format.

To produce atom results, you would want the have products_controller.rb respond_to format.atom, and to have the url end with atom: http://localhost:3000/products/2/who_bought.atom.

By adding more format statements to products_controller.rb, you can support more formats.

Click on the two products_controller links in the above paragraphs to see examples.

Generic-user-small
12 Oct 2011, 18:32
Rob (2 posts)

Ah thanks for the tip! My controller was setup to respond to .atom not .xml. Should have read that curl closer :D

You must be logged in to comment