我使用了来自this RailsCasts episode的版本化API设计并且它工作得很好,除了我的jbuilder模板没有呈现。
这是我的控制器代码:
module Api
module V1
class LocationsController < ApplicationController
respond_to :json
def index
@locations = Location.all
end
end
end
end
我的jbuilder文件位于app/views/api/v1/locations/index.json.jbuilder
我收到以下错误:
ActionView::MissingTemplate (Missing template api/v1/locations/index,
api/v1/api/index, application/index with {:locale=>[:en], :formats=>[:json],
:variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Searched in: * "/XXX/XXX/XXX/XXX/app/views"
编辑:以下是routes.rb
文件中的代码:
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :locations, only: [ :index ]
end
end
我有什么想法可以调试这个吗?为什么Rails找不到api
文件夹中的文件?谢谢!
答案 0 :(得分:0)
我一直都有这个问题。它有时可能是json模板中实际代码中出现的拼写错误或错误。尝试注释掉index.json.jbuilder文件中的所有行,重新加载,工作?尝试一次取消注释一行。我还没有找到一个解决方案,可以用jbuilder来更好地调试这个问题......
答案 1 :(得分:0)
在路线中你应该删除默认值:{format :: json}