【问题标题】:ActionView::MissingTemplate error when using modules使用模块时出现 ActionView::MissingTemplate 错误
【发布时间】: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


【解决方案1】:

我一直有这个问题。它有时可能是 json 模板中的实际代码中出现的拼写错误或错误。尝试注释掉 index.json.jbuilder 文件中的所有行,重新加载,工作吗?尝试一次取消注释一行。我还没有找到更好地使用 jbuilder 调试此问题的解决方案...。

【讨论】:

    【解决方案2】:

    在路由中你应该删除默认值:{ format: :json }

    【讨论】:

    • 即使这可能是真的(这里不是 ruby​​ 专家),您也应该尝试使用更多信息来扩展您的答案,以便为 OP 提供更多内容。
    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多