【发布时间】:2015-01-08 00:02:52
【问题描述】:
我正在使用来自 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 文件夹中的文件?谢谢!
【问题讨论】:
-
我认为在 Rails 4 中,您必须在控制器中执行
respond_with @locations。 -
您能否展示与此控制器相关的
routes.rb文件? -
@Anthony 我相信只是在对象上调用
to_json(或者是as_json?),因为调用实际上并没有呈现jbuilder模板。 -
这段代码看起来没什么问题,但在错误消息中
api/v1/api/index似乎不在通常的模板路径中。您的代码中是否还有其他空间设置?这个控制器在app/controllers/api/v1/中吗?
标签: ruby-on-rails ruby actionview