这是我的测试:
require 'test_helper'
class CreatingZombiesTest < ActionDispatch::IntegrationTest
setup { host! 'api.example.com' }
test 'creates a new zombie' do
post '/zombies',
{ zombie:
{ name: 'brain_eater', weapon: 'teeth'}
}.to_json,
{ 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s }
assert_equal 201, response.status
end
end主计长:
def create
@zombie = Zombie.new(zombie_params)
if @zombie.save
render json: @zombie, status: 201, location: @zombie
end
end我不断地发现这个错误,搜索是没用的。
CreatingZombiesTest#test_creates_a_new_zombie:
NoMethodError: undefined method `zombie_url' for #<Api::ZombiesController:0x007f8ef9f6cd60>
app/controllers/api/zombies_controller.rb:22:in `create路线:
Prefix Verb URI Pattern Controller#Action
api_zombies GET /zombies(.:format) api/zombies#index {:subdomain=>"api"}
POST /zombies(.:format) api/zombies#create {:subdomain=>"api"}
new_api_zombie GET /zombies/new(.:format) api/zombies#new {:subdomain=>"api"}
edit_api_zombie GET /zombies/:id/edit(.:format) api/zombies#edit {:subdomain=>"api"}
api_zombie GET /zombies/:id(.:format) api/zombies#show {:subdomain=>"api"}
PATCH /zombies/:id(.:format) api/zombies#update {:subdomain=>"api"}
PUT /zombies/:id(.:format) api/zombies#update {:subdomain=>"api"}
DELETE /zombies/:id(.:format) api/zombies#destroy {:subdomain=>"api"}发布于 2015-02-19 14:51:59
好吧,我解决了,应该是
location: api_zombie_url(zombie)我需要路由中的api_,因为我的控制器在Api命名空间中,所以没有定义zombie_url,但是定义了api_zombie_url。
发布于 2015-02-19 14:29:48
你设置routes.rb了吗?ie:
post 'zombie/create'跑
rake routes在您的终端并得到相应的链接。注-如果您的链接是zombie_create,那么您将不得不追加路径或url,这取决于您要完成的任务。(zombie_create_url或zombie_create_path)
https://stackoverflow.com/questions/28608499
复制相似问题