NoMethodError:未定义的方法`zombie_url'

时间:2015-02-19 14:10:07

标签: ruby-on-rails rspec tdd

这是我的测试:

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"}

2 个答案:

答案 0 :(得分:2)

你设置了routes.rb吗?即:

 post 'zombie/create'

运行

rake routes

在您的终端中获取相应的链接。注意 - 如果您的链接是zombie_create,那么您将必须附加路径或URL,具体取决于您要完成的操作。 (zombie_create_url或zombie_create_path)

答案 1 :(得分:0)

好的,我解决了, 它应该是

location: api_zombie_url(zombie)

我的路由中需要api_,因为我的控制器位于Api命名空间内,因此未定义zombie_url,但定义了api_zombie_url。