Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec返回404;使用JSON数据发布到子域_Ruby On Rails_Json_Ruby_Rspec - Fatal编程技术网

Ruby on rails RSpec返回404;使用JSON数据发布到子域

Ruby on rails RSpec返回404;使用JSON数据发布到子域,ruby-on-rails,json,ruby,rspec,Ruby On Rails,Json,Ruby,Rspec,我试图在请求Rspec中发送原始JSON。(因为我在控制器Rspec中尝试过,但在rails 4.2中没有解析JSON数据。) 我还设置了子域api 我总是得到404 describe "POST #create" do let(:json_body) do '{"foo":"foo_value", "bar":"bar_value", "baz":"baz_value"}' end it "returns not acceptable to non js

我试图在请求Rspec中发送原始JSON。(因为我在控制器Rspec中尝试过,但在rails 4.2中没有解析JSON数据。) 我还设置了子域api

我总是得到
404

  describe "POST #create" do
    let(:json_body) do
      '{"foo":"foo_value", "bar":"bar_value", "baz":"baz_value"}'
    end
    it "returns not acceptable to non json content-type request" do
      post "/subscriptions" , json_body, { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
    end
有办法解决这个问题吗

更新

config/routes.rb

require 'api_constraints'

Rails.application.routes.draw do
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      resources :subscriptions, only: [:index, :new, :create]
    end
  end

end
更新2

我设置了
host!'api.localhost.local'
,然后它就可以工作了。 裁判: 参考文献2:

我还想知道,为什么一旦我用子域设置了这样的路由,它会将
.localhost.local:3000
作为默认路由?

需要“rails\u helper”
require 'rails_helper'

RSpec.describe Api::V1::SubscriptionsController, type: :request do
  let(:json_body) do
    '{:foo: 12, bar: 33}'
  end
  it "returns not acceptable to non json content-type request" do
    host! 'api.lvh.me' # <= This is needed because 'api' is the subdomain.
    headers = {
      "ACCEPT" => "application/json",     # This is what Rails 4 accepts
      'CONTENT_TYPE' => 'application/json'
    }

    post "/subscriptions", json_body.to_json ,headers
  end
end
RSpec.description Api::V1::SubscriptionController,类型::请求do 让(:json_body)去做 “{:foo:12,bar:33}” 结束 它“返回非json内容类型请求不可接受”do 主人api.lvh.me'#“application/json”#这是Rails 4所接受的 “内容类型”=>“应用程序/json” } post“/subscriptions”,json_body.to_json,标题 结束 结束
需要“rails\u助手”
RSpec.description Api::V1::SubscriptionController,类型::请求do
让(:json_body)去做
“{:foo:12,bar:33}”
结束
它“返回非json内容类型请求不可接受”do
主人api.lvh.me'#“application/json”#这是Rails 4所接受的
“内容类型”=>“应用程序/json”
}
post“/subscriptions”,json_body.to_json,标题
结束
结束

如果你使用json_body作为
{foo::foo,bar::bar}
怎么办?你能发布你的routes.rb,controller的具体部分以及你如何设置子域吗?请尝试
{'Content Type'=>'application/json','Accept'=>'application/json'}
@AndreyDeineko抱歉回复太晚了。它返回
400
@zettetic我得到的东西没有改变。如果你使用json作为
{foo::foo,bar::bar}
?你能发布你的routes.rb的具体部分吗,控制器和你如何设置子域?试试
{'Content Type'=>'application/json',Accept'=>'application/json'}
@AndreyDeineko很抱歉回复太晚。它返回
400
@zettetic我得到的东西没有改变。
require 'rails_helper'

RSpec.describe Api::V1::SubscriptionsController, type: :request do
  let(:json_body) do
    '{:foo: 12, bar: 33}'
  end
  it "returns not acceptable to non json content-type request" do
    host! 'api.lvh.me' # <= This is needed because 'api' is the subdomain.
    headers = {
      "ACCEPT" => "application/json",     # This is what Rails 4 accepts
      'CONTENT_TYPE' => 'application/json'
    }

    post "/subscriptions", json_body.to_json ,headers
  end
end