ruby-on-railsfullcalendarruby-on-rails-5jbuilder

Rails Missing JBuilder File Template


I've been trying to use Fullcalendar to display events from a JSON feed. The events in the JSON feed are generated by SchoolEventsController. However, the events won't show up on Fullcalendar. Also, viewing the JSON file(school_events/index.json.jbuilder) produces this error:

ActionController::UnknownFormat at /calendar.json SchoolEventsController#index is missing a template for this request format and variant. request.formats: ["application/json"] request.variant: []

I think I have narrowed my problem down to these two files:

index function in school_events_controller.rb:

class SchoolEventsController < ApplicationController

    def index
        @events = @calendar.events
        respond_to do |format|
            format.html # index.html.erb
            format.json # index.json.jbuilder?
        end
    end

end

school_events/index.json.jbuilder:

json.array!(@events) do |event|
    date_format = event.all_day? ? '%Y-%m-%d' : '%Y-%m-%dT%H:%M:%S'
    json.id event.id
    json.title event.name
    json.start event.start_time.strftime(date_format)
    json.end event.end_time.strftime(date_format)
    json.url event_url(event, format: :html)
end

Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
  "https://github.com/#{repo_name}.git"
end

ruby '2.3.1'
gem 'rails', '~> 5.0.2'
gem 'mime-types', '~> 3.1'
gem 'paperclip', '~> 5.0'
gem 'rmagick', '~> 2.15'
gem 'friendly_id', '~> 5.1'
gem 'closure_tree', '~> 6.0'
gem 'ancestry',     '~> 3.0'
gem 'ranked-model', '~> 0.4.0'
gem 'mailgun_rails', '0.9.0'
gem 'nested_form', '0.3.2'
gem 'event-calendar', github: 'kirandc/event_calendar', require: 'event_calendar'
gem 'jbuilder', '~> 1.2'
gem 'ransack', '~> 1.7'
gem 'sprockets-rails', '~> 3.1'
gem 'flash_cookie_session', github: 'trevorturk/flash_cookie_session'
gem 'date_validator', '~> 0.9.0'
gem 'jquery-rails',        '~> 4.1'
gem 'jquery-ui-rails',     '~> 6.0'
gem 'jquery_mobile_rails', '~> 1.3.2'
gem 'fancybox2-rails', github: 'ChallahuAkbar/fancybox2-rails', tag: 'v0.3.0'
gem 'kaminari', '~> 1.0'
gem 'redcarpet', '~> 3.3'
gem 'pg', '~> 0.21.0'
gem 'pghero', '~> 1.2'
gem 'autoprefixer-rails', '~> 7.0'
gem 'bourbon',            '~> 4.2'
gem 'normalize-rails',    '~> 4.1'
gem 'sass-rails',         '~> 5.0'
gem 'uglifier',           '~> 3.0'
gem 'bcrypt', '~> 3.1', require: false
gem 'google-api-client', '~> 0.13.0'
gem 'recaptcha', '~> 4.1', require: 'recaptcha/rails'
gem 'yajl-ruby', '~> 1.3', require: false
gem 'tinymce-rails', '~> 4.3'
gem 'turbolinks', '~> 5.0'
gem 'sunspot_rails', '~> 2.2'
gem 'image_optim',      '~> 0.25.0'
gem 'image_optim_pack', '~> 0.5.0'

group :production do
  gem 'redis-rails', '~> 5'
  gem 'sitemap_generator', '~> 5.1'
  gem 'newrelic_rpm', '~> 4.0'
  gem 'rollbar', '~> 2.11'
end

group :development, :production do
  gem 'rack-mini-profiler', '~> 0.10.1'
  gem 'whenever', '~> 0.9.4', require: false
end

group :development do
  gem 'capistrano',               '~> 3.7',   require: false
  gem 'capistrano-bundler',       '~> 1.1',   require: false
  gem 'capistrano-faster-assets', '~> 1.0',   require: false
  gem 'capistrano-rails',         '~> 1.1',   require: false
  gem 'better_errors',     '~> 2.1'
  gem 'binding_of_caller', '~> 0.7.2'
  gem 'thin', '~> 1.6'
  gem 'guard',            '~> 2.13'
  gem 'guard-livereload', '~> 2.5', require: false
  gem 'brakeman', '~> 3.3', require: false
  gem 'pry', '~> 0.10.3'
  gem 'letter_opener', '~> 1.4'
  gem 'yard', '~> 0.9.5'
  gem 'bullet', '~> 5.0'
  gem 'rubocop',       '~> 0.4',    require: false
  gem 'rubocop-rspec', '~> 1.7',    require: false
  gem 'scss_lint',     '~> 0.54.0', require: false
  gem 'progress_bar', '~> 1.0'
  gem 'sunspot_solr', '~> 2.2'
end

group :development, :test do
  gem 'spring-commands-rspec',   '~> 1.0'
  gem 'spring-commands-rubocop', '~> 0.2.0'
  gem 'rails-controller-testing', '~> 1.0.1'
  gem 'guard-rspec', '~> 4.6'
  gem 'rspec',       '~> 3.4'
  gem 'rspec-rails', '~> 3.4'
end

group :test do
  gem 'capybara', '~> 2.7'
  gem 'factory_girl_rails', '~> 4.7'
  gem 'shoulda-matchers', '~> 3.1', require: false
  gem 'launchy', '~> 2.4'
  gem 'simplecov', '~> 0.14.0', require: false
  gem 'database_cleaner', '~> 1.5', require: false
  gem 'webmock', '~> 3.0'
  gem 'accept_values_for', '~> 0.7.2'
end

relevant routes in routes.rb:

scope defaults: { calendar_name: 'School Calendar' } do
    get '/calendar/list',             to: 'school_events#list',    as: :school_calendar_list
    get '/calendar/editors',          to: 'school_events#editors', as: :editors_school_calendar
    get '/calendar(/:year(/:month))', to: 'school_events#index',
                                      as: :school_calendar,
                                      constraints: { year: /20(?:1[1-9]|20)/, month: /\d{1,2}/ }
    resources :school_events, path: '/events', except: [:index]
  end

  scope '/calendar' do
    get '/edit', to: 'school_events#edit_calendar',
                 defaults: { calendar_name: 'School Calendar' },
                 as: :edit_school_calendar
    put '/',     to: 'school_events#update_calendar',
                 defaults: { calendar_name: 'School Calendar' }

    resources :google_syncs, only: %i[new create destroy], as: :school_calendar_google_syncs do
      get :authorize, on: :collection
    end
  end

When I replace format.json in SchoolEventsController with format.json { render json: @events }, calendar.json will output(in JSON) all the desired events from my database. I don't want to use those outputs because I would need a messy workaround to rename all the attributes to comply with Fullcalendar's requirements. Does anyone have a way to fix this problem? Thanks in advance!

Note: I'm using Rails 5


Solution

  • $ bundle exec spring stop
    $ bundle install
    $ bundle exec rails s
    

    jbuilder has an initialization hook that defines the .jbuilder template handler. Without it, Rails doesn't know what to do with .jbuilder template files. The initializer isn't getting run, maybe because spring never restarted when jbuilder was installed, or jbuilder wasn't installed.