I have a problem very similar to the one mentioned in this question. However, none of the solutions described in the answers are working.
When trying to access my route, with or without .json extension, as well as with jQuery $.ajax and $.getJSON (with the correct Accept headers), I am getting the following exception:
Missing template users/events/index, application/index with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:json], :locale=>[:en, :en]}.
Since I only need JSON responses for this controller, I don’t have nor want any templates for this controller.
My controller is defined like so:
class Users::EventsController < ApplicationController
respond_to(:json)
def index
# Some extra code here...
respond_with(@data)
end
end
With these routes in place (under a “user” scope):
resources(:events, :only => [:index, :show]) do
collection do
get ':year/:month', :to => 'events#index', :format => :json
end
end
The problem persists when adding the do |format| block and explicitly making it render as JSON.
Tried this on both Rails 3.1.1 and on 3.2 and got the same issue.
It looks like a server-side issue since accessing with “.json” doesn’t work. What am I missing?
After a bit more tinkering I found out what was the actual cause of this problem.
At some point in the controller I had the following code block:
The
returnkeyword was incorrectly used and caused the entire action to return ahead of time, causing the issue.