我有一条看起来像这样的路线:
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)
答案 0 :(得分:0)
愚蠢的我。我以为Rails会自动将jbuilder
宝石添加到我的Gemfile
中,但是事实证明,直到Rails 3.2才发生。我正在使用的这个应用程序是一个旧的Rails 3应用程序,已升级到3.2,因此jbuilder
gem不在我的Gemfile中。
一旦我添加了它,它就像一个饰物。