Rails 缺少 JBuilder 文件模板

问题描述 投票:0回答:3

我一直在尝试使用 Fullcalendar 来显示来自 JSON 提要的事件。 JSON 提要中的事件由 SchoolEventsController 生成。但是,事件不会显示在 Fullcalendar 上。此外,查看 JSON 文件 (school_events/index.json.jbuilder) 会产生此错误:

ActionController::UnknownFormat 在 /calendar.json SchoolEventsController#index 缺少此请求格式和变体的模板。 request.formats: ["应用程序/json"] 请求变体:[]

我想我已经将问题缩小到这两个文件:

school_events_controller.rb中的index函数

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

宝石文件

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

相关路线在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

当我用

format.json
替换 SchoolEventsController 中的
format.json { render json: @events }
时,calendar.json 将从我的数据库中输出(以 JSON 格式)所有需要的事件。我不想使用这些输出,因为我需要一个混乱的解决方法来重命名所有属性以符合 Fullcalendar 的要求。有没有人有办法解决这个问题?提前致谢!

注意:我使用的是 Rails 5

ruby-on-rails fullcalendar ruby-on-rails-5 jbuilder
3个回答
2
投票
$ bundle exec spring stop
$ bundle install
$ bundle exec rails s

jbuilder
有一个初始化挂钩,它定义了
.jbuilder
模板处理程序。没有它,Rails 不知道如何处理
.jbuilder
模板文件。初始化程序没有运行,可能是因为
spring
在安装
jbuilder
时从未重新启动,或者
jbuilder
没有安装。


2
投票

Rails 7?

如果您要升级到 Rails 7,此错误的一个可能来源是您需要指定模板的格式类型:

# in my view
render(template: "line_items/line_items", formats: :json)  # must specify json format

这是一个小细节,但可以让人困惑好几个小时。


0
投票

与要求的内容没有特别相关,但由于运行请求规范时我的 Rails API-only 应用程序出现同样的错误,我来到这里。

错误:

Failure/Error: get url
     
     ActionController::UnknownFormat:
       Api::V1::TodosController#index is missing a template for this request format and variant.
     
       request.formats: ["text/html"]
       request.variant: []

因此,在运行请求规范时,您必须指定标头,如下所示:

get url, headers: { "Content-Type" => "application/json", "Accept" => "application/json" }

您甚至可以将其提取到 ./spec/support/request_helpers.rb 以像

headers
一样使用它:

# spec/support/request_helpers.rb
module RequestHelpers
  def headers
     { "Content-Type" => "application/json", "Accept" => "application/json" }
  end
end

RSpec.configure do |config|
  config.include RequestHelpers, type: :request
end

只是希望能帮助更多像我一样的新手!

© www.soinside.com 2019 - 2024. All rights reserved.