如何在“创建”操作后发送位置
How to send location after `create` action
我有以下 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
这是因为命名空间,对location
使用[:api, episode]
我有以下 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
这是因为命名空间,对location
[:api, episode]