I have a Rails 3.1 application that uses devise for authentication. I am trying to make a call via ajax to a url. For some reason I keep getting 401 Unauthorized access. I have followed the tutorial here http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery on how to authenticate jQuery requests. The code for the request is
$.get('/chromosomes.json', {sequence_start: '10', sequence_end: '100'},
function(data){
alert(data)
}, 'json');
What is strange is that on the server I see
Started GET “/chromosomes.json?sequence_start=10&sequence_end=100&_=1320958293654” for 127.0.0.1 at Thu Nov 10 15:51:33 -0500 2011
Processing by ChromosomesController#index as JSON
Parameters: {“sequence_start”=>”10”, “_”=>”1320958293654”, “sequence_end”=>”100”}
Completed in 10ms
In my controller I have:
before_filter :authenticate_user!
def index
respond_to do |format|
format.html
format.json { render :json => @chromosomes }
end
end
For some reason the authenticity session token is not being passed to Rails. If I remove the authenticate_user! before filter it works fine!? Any help is very much appreciated. Thanks!
I fixed this issue by adding :token_authenticatable to my user model. This allows users to be authenticated by session tokens.