我正在使用backbone.js和rails。我的控制器接受並返回JSON。我的所有測試都正常工作,直到我添加了設計認證。我不能讓黃瓜保持認證。設計,黃瓜和水豚不在JSON請求上進行驗證
我已經試過各種上提到:
- devise wiki
- here - 標準登錄(進入頁面,填寫表格,單擊按鈕),設置cookie,並有過濾器之前,一個特殊的應用,做但後者在機架(無法得到這個甚至可以運行)
- 做上述的Cookie /使用的before_filter this set_cookie method
一切都無濟於事。登錄返回罰款(200),但是當我發送我的請求時,我得到一個401響應。
這裏是我的控制器代碼:
class ContactsController < ApplicationController
before_filter :authenticate_user!
### backbone
def index
render :json => current_user.person.contacts
end
def show
render :json => current_user.person.contacts.find(params[:id])
end
def create
contact = current_user.person.contacts.create! params
render :json => contact
end
def update
contact = current_user.person.contacts.find(params[:id])
contact.update_attributes! params
render :json => contact
end
def destroy
contact = current_user.person.contacts.find(params[:id])
contact.destroy
render :json => contact #TODO: change to render nothing
end
這裏是一個黃瓜特點:
Scenario: View contacts
Given I am logged in as "[email protected]"
And I send and accept JSON
And the following contacts:
|name|
|name 1|
|name 2|
|name 3|
|name 4|
When I send a GET request for "/contacts"
Then the response should be "200"
And the JSON response should be an array with 4 "name" elements
And I should get the following contacts:
|name|
|name 1|
|name 2|
|name 3|
|name 4|
的JSON的方法是直接用機架。我懷疑這是我的問題所在,但我不知道如何克服它。
這裏是黃瓜JSON步驟:
World(Rack::Test::Methods)
Given /^I send and accept JSON$/ do
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
end
When /^I send a GET request for "([^\"]*)"$/ do |path|
get path
end
When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, table|
params = table.hashes.flatten[0].to_json
post path, params
end
When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, table|
params = table.hashes.flatten[0].to_json
put path, params
end
When /^I send a DELETE request to "([^\"]*)"$/ do |path|
delete path
end
Then /^the response should be "([^\"]*)"$/ do |status|
last_response.status.should == status.to_i
end
Then /^the JSON response should have 1 "([^\"]*)" element$/ do |name|
page = JSON.parse(last_response.body)
page[name].should be
end
Then /^the JSON response should be an array with (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
page = JSON.parse(last_response.body)
page.map { |d| d[name] }.length.should == number_of_children.to_i
end