vendredi 14 décembre 2018

Why is Rails not finding my jbuilder template or not rendering it?

I have a route that looks like this:

resources :property_searches, :path => 'search'

That generates these routes:

 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

This is what I have in my 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

Then in my views/property_searches/index.json.jbuilder, I have the following:

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

When I visit /search.json in my address bar, this is what I get in my logs:

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'

I also tried putting this in my respond_to block instead:

  format.json { render json: {properties: []} }

And even though it doesn't produce the same error, when I go to the page I literally just see this on the page:

{"properties":[]}

It doesn't show me the values within @properties that I set.

What is causing this and how do I fix it?

Aucun commentaire:

Enregistrer un commentaire