为啥 Rails 找不到我的 jbuilder 模板或不渲染它?

Posted

技术标签:

【中文标题】为啥 Rails 找不到我的 jbuilder 模板或不渲染它?【英文标题】:Why is Rails not finding my jbuilder template or not rendering it?为什么 Rails 找不到我的 jbuilder 模板或不渲染它? 【发布时间】:2019-05-16 02:15:21 【问题描述】:

我的路线如下所示:

resources :property_searches, :path => 'search'

生成这些路由:

 property_searches GET    /search(.:format)                                                             property_searches#index
                   POST   /search(.:format)                                                             property_searches#create
new_property_search GET    /search/new(.:format)                                                         property_searches#new
edit_property_search GET    /search/:id/edit(.:format)                                                    property_searches#edit
    property_search GET    /search/:id(.:format)                                                         property_searches#show
                    PUT    /search/:id(.:format)                                                         property_searches#update
                    DELETE /search/:id(.:format)                                                         property_searches#destroy

这就是我的PropertySearchesController#Index

@properties = Property.first(5) #This is just a test

respond_to do |format|
  format.html 

  format.json 

  format.fullsearch do
    render :formats => [ :js ]
  end

  format.livesearch do

  end

  format.filtersearch do
    render :formats => [ :quicksearch ]
  end
end

然后在我的views/property_searches/index.json.jbuilder 中,我有以下内容:

json.properties do
  json.array!(@properties) do |property|
    json.name property.name
  end
end

当我在地址栏中访问/search.json 时,我在日志中得到以下信息:

Started GET "/search.json" for 127.0.0.1 at 2018-12-14 14:22:32 -0500
Processing by PropertySearchesController#index as JSON

Completed 500 Internal Server Error in 993.8ms
** [Raven] Missing template property_searches/index, application/index with :locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :haml]. Searched in:
  * "/hj-project/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/kaminari-0.15.1/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/comfortable_mexican_sofa-1.8.5/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/formatted_form-2.1.2/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/declarative_authorization-0.5.7/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/bundler/gems/comfy-blog-fcf9e4e88948/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/xray-rails-0.3.1/app/views"
 excluded from capture due to environment or should_capture callback

ActionView::MissingTemplate (Missing template property_searches/index, application/index with :locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :haml]. Searched in:
  * "/hj-project/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/kaminari-0.15.1/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/comfortable_mexican_sofa-1.8.5/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/formatted_form-2.1.2/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/declarative_authorization-0.5.7/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/bundler/gems/comfy-blog-fcf9e4e88948/app/views"
  * "/.rvm/gems/ruby-2.3.0@hjproject/gems/xray-rails-0.3.1/app/views"
):
  actionpack (3.2.22.5) lib/action_view/path_set.rb:58:in `find'

我也尝试将它放在我的 respond_to 块中:

  format.json  render json: properties: [] 

即使它不会产生相同的错误,当我转到该页面时,我实际上只是在页面上看到这个:

"properties":[]

它没有显示我设置的@properties 中的值。

这是什么原因造成的,我该如何解决?

更新 1

当我直接渲染集合时,它可以工作,即当我这样做时:

format.json  render json: @properties 

但是,这不会对其进行迭代。它只是吐出值,日志如下所示:

Started GET "/search.json" for 127.0.0.1 at 2018-12-14 14:50:28 -0500
Processing by PropertySearchesController#index as JSON
   (0.5ms)  SELECT COUNT(*) FROM "admins" WHERE (admins.superadmin = 't')
  PropertyCurrencyType Load (0.4ms)  SELECT "property_currency_types".* FROM "property_currency_types" WHERE "property_currency_types"."name" = 'USD' LIMIT 1
.
.
.
  Agent Load (0.3ms)  SELECT "agents".* FROM "agents" WHERE "agents"."id" = $1 LIMIT 1  [["id", 0]]
  Property Load (1.2ms)  SELECT "properties".* FROM "properties" LIMIT 5
Completed 200 OK in 2574.6ms (Views: 8.8ms | ActiveRecord: 595.1ms)

【问题讨论】:

【参考方案1】:

我傻了。我假设 Rails 已经自动将jbuilder gem 添加到我的Gemfile 中,但事实证明它直到 Rails 3.2 才发生。我正在使用的这个应用程序是升级到 3.2 的旧版 Rails 3 应用程序,所以 jbuilder gem 不在我的 Gemfile 中。

一旦我添加它,它就像一个魅力。

【讨论】:

以上是关于为啥 Rails 找不到我的 jbuilder 模板或不渲染它?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Rails 找不到我的资产?

如何在 Jbuilder 中使用带有 Gon gem 的 Rails 助手?

使用 Jbuilder(或其他)的 Rails JSON API 布局

Rails jbuilder DateTime将小数添加到秒

如何使用 Jbuilder & Rails 生成自定义 json 响应

请问vegas为啥会无法渲染?以前渲染都成功的,这次就一直报错,根据网上的方法找不到对应的编码模