gpt4 book ai didi

ruby-on-rails - ActionView::MissingTemplate,带有 JSON 的 Rails 5 API

转载 作者:数据小太阳 更新时间:2023-10-29 07:31:40 25 4
gpt4 key购买 nike

尝试在我的 Rails 5 Api 中呈现 JSON 时,我一直收到 ActionView::MissingTemplate 错误。我只想呈现纯 JSON,没有 jbuilder 或其他 View 。谁能帮忙?

thing_controller.rb:

class Api::ThingController < ApplicationController
def thing
render json: {error: 'This is my error message.'}, status: 422
end
end

thing_controller_test.rb:

require 'test_helper'
class Api::ThingControllerTest < ActionDispatch::IntegrationTest
test "the truth" do
get '/api/thing'
assert_response 422
end
end

完整错误信息:

Error: Api::ThingControllerTest#test_the_truth: ActionView::MissingTemplate: Missing template api/thing/thing, application/thing with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

应用程序 Controller .rb:

 class ApplicationController < ActionController::API
include ActionController::Caching
include ActionController::ImplicitRender # want implicit view rendering for JBuilder

before_action :add_cors_headers


def options
head(:ok) if request.request_method == "OPTIONS"
end

最佳答案

这与 an issue 有关在 Rails 5 beta ActionController::API 和 Jbuilder 中。看起来它已被此 pull request 修复.

同时您可以返回纯文本并设置内容类型,如下所示:

render plain: {error: 'This is my error message.'}.to_json, status: 422, content_type: 'application/json'

关于ruby-on-rails - ActionView::MissingTemplate,带有 JSON 的 Rails 5 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36870814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com