如何在`create`动作后发送位置

时间:2016-06-15 17:36:05

标签: ruby-on-rails ruby ruby-on-rails-4 rails-api

我有以下create行动:

def create
  episode = Episode.new(episode_params)
  if episode.save
    render json: episode, status: :created, location: episode
  end
end

但是当我测试以下内容时:

require 'test_helper'

class CreatingEpisodesTest < ActionDispatch::IntegrationTest
  setup { host! 'api.example.com'}

  test 'create episodes' do
    post '/episodes',
      { episode:
          { title: 'Bananas', description: 'Learn about bananas.' }
      }.to_json,
      { 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s }

    assert_equal 200, response.status
    assert_equal Mime::JSON, response.content_type

    episode = json(response.body)
    assert_equal "/episodes/#{episode[:id]}", response.location
  end
end

我收到以下错误:

1) Error: CreatingEpisodesTest#test_create_episodes: NoMethodError: undefined method `episode_url' for #<API::EpisodesController:0x007f8e34519450> Did you mean?  episode_params
app/controllers/api/episodes_controller.rb:11:in `create'
test/integration/creating_episodes_test.rb:7:in `block in #<class:CreatingEpisodesTest>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

我是客人,在创建剧集后,我想念一些发送位置。

更新:路线

Rails.application.routes.draw do
  namespace :api, path: '/', constraints: { subdomain: 'api' } do
    resources :zombies
    resources :episodes
  end
end

1 个答案:

答案 0 :(得分:1)

这是因为名称空间,[:api, episode]

使用location