Javascript Jbuilder在RubyonRails中使用对API的POST请求

Javascript Jbuilder在RubyonRails中使用对API的POST请求,javascript,ruby-on-rails,json,fetch,jbuilder,Javascript,Ruby On Rails,Json,Fetch,Jbuilder,我尝试在我的项目中使用Rails和ReactJS来划分后端和前端 我有一个函数可以对POST请求进行异步处理,并使用gem 生成JSON API的步骤 JavaScript: fetch('shops/1/deals',{ method: "POST", body: JSON.stringify({shop_id: 1, deals_type: "workout"}), headers: { 'Accept': 'application/json'

我尝试在我的项目中使用Rails和ReactJS来划分后端和前端

我有一个函数可以对
POST
请求进行异步处理,并使用gem 生成JSON API的步骤

JavaScript:

fetch('shops/1/deals',{
      method: "POST",
      body: JSON.stringify({shop_id: 1, deals_type: "workout"}),
      headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
      },
    })
    .then(function(res){ 
      console.log(res)
      res.json().then(function(data){
       alert( JSON.stringify( data ) ) 
     })
})
控制器:

def index 
 @shop = Shop.find(params[:id])
 @deals = @shop.deals
end

def create
 @deal = Deal.new(deal_params)
 respond_to do |format|
  if @deal.save
   format.json { render :show, status: :created, location: @deal }
  else
   format.json { render json: @deal.errors, status: :unprocessable_entity }
  end
 end
end
如果我在
视图/deals
中有
\u deal.json.jbuilder
文件

json.extract! deal, :id, :shop_id, :deals_type, :created_at, :updated_at, :deal_time
我将获得警报
{id:number,shop\u id:1,deals\u type:“锻炼”,…}

但是我删除了
\u deal.json.jbuilder
文件,我将得到
{}
null对象

为什么会出现此问题?

此行:

format.json { render :show, status: :created, location: @deal }
告诉Rails为JSON呈现您的
show
视图,这可能会加载您的
app/views/deals/show.JSON.jbuilder
视图;该视图本身可能只是使您的交易部分化。删除分部会导致视图定义无效

因此,除非您还准备更新引用该部分的模板,否则不要删除该部分