渲染 :json 没有模板
Render :json without a template
据我所知,json
可以在没有模板的情况下在 Rails 上呈现,使用如下代码:
module Overrides
class SessionsController < DeviseTokenAuth::SessionsController
def destroy
render json: {
errors: 'Test 200'
}, status: 200
end
我还尝试了以下操作:
respond_to do |format|
format.json { render :json => {success:true}, status: 200}
end
没有结果。我仍然遇到同样的错误。
Started DELETE "/auth/sign_out" for 127.0.0.1 at 2016-04-28 20:29:27 +0300
Processing by Overrides::SessionsController#destroy as json
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."uid" = LIMIT [["uid", "demo"], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (0.5ms) UPDATE "users" SET "tokens" = , "updated_at" = WHERE "users"."id" = [["tokens", "{\"xxxxx\":{\"token\":\"a$DvZoMNnonCc7QIPxUGgl2uVgY6ufM4dsdnD7PJxBkdDUJoY9cOXYS\",\"expiry\":1463064626,\"last_token\":\"a$bjFBVZanZhFRnh7D37/X/Or40I4IRSDP/fkzI3Uihucsa8/u.grNm\",\"updated_at\":\"2016-04-28T17:50:26.524+03:00\"},\"a85-6Pqg04JKNheGxUzaXQ\":{\"token\":\"a$Bmeo9N9CbUeU7S9nnGYLGukzNuBfx6aTlLjR5Lqp7XK0yNldCcVSe\",\"expiry\":1463064559},\"LQquXUPX8XYX2iT9cb5big\":{\"token\":\"aIEBKFHUmJH8stSg4erzl.7M/WersmemSgifGm97JAHPSM3QeIhVW\",\"expiry\":1463064626},\"o1ihjtFl80sXmVwRQpylFg\":{\"token\":\"a$FGLKBBipRM8KGUft6.KtQ.1C.4LEr3RV0nX3FsEwtSf3xNgIgBNsK\",\"expiry\":1463064845,\"last_token\":\"arKzo.BwG2eDYU/DUJiN/.D.jbfhlTUO1N1JyPJMtDoCUWZh.ToSa\",\"updated_at\":\"2016-04-28T17:54:05.714+03:00\"},\"default\":{\"token\":\"a$Q3Kpj2K9d.r22BavXNbFSu/swmKsjETvGzwiPe5OUW3OZ0Vx8MaX2\",\"expiry\":1463066712,\"last_token\":\"a$bNfn2noq6iIAelbrOsIZcum7z9aYtghtQzFu6.300zFaYM4N8BtAO\",\"updated_at\":\"2016-04-28T18:25:12.081+03:00\"},\"0Yb2s4qO8whrJftlZdJS3A\":{\"token\":\"a$kGj0h6VTRcb3.36jwvKU8eRcTElFjbY8WVofLOsRbzN8w2wYww0vi\",\"expiry\":1463066405,\"last_token\":\"a$dJso8ZQBJlL5KMPmoB5fROvZr5gIUEbiWP939L/DqGkDkjXSQ2xEC\",\"updated_at\":\"2016-04-28T18:20:05.611+03:00\"},\"3n866Hx7t4GXC8PasANnng\":{\"token\":\"a$uizgREN5xUQDrDutGNF9vu/v.obbj3O.2uF2WWLTeEcUaf51Aiore\",\"expiry\":1463066450,\"last_token\":\"a$rGw/ixMlnkA4Nn1p3qlQiORuMnxBcSZaOVGJaLWNqvG2HAh/uaxry\",\"updated_at\":\"2016-04-28T18:21:03.968+03:00\"},\"xqZiCAeG9qNttkIoH5wC9g\":{\"token\":\"a$XLu75dE44rv.avuesESY3evNLYUP9m.MnxBdFbgZ07ssFbPl1uLLu\",\"expiry\":1463066555,\"last_token\":\"a$v1z4lz/xPceYKo6jjLO7uej3fUTWNQ99Qb/2C8kK7hXPEvaNSoqm.\",\"updated_at\":\"2016-04-28T18:22:35.889+03:00\"},\"dbeTgN6KLQxWKhETjSjKbw\":{\"token\":\"a$o122yFjcZW7bZkyigKEUNeldwHBnlqALzqzvl11G3gCm50KWTkkuO\",\"expiry\":1463066693,\"last_token\":\"a$oHIZ87V4RNxBi6DF8pl2ReAXfct3m3E/Iuvbqe.hs8xeEH9.dxdBu\",\"updated_at\":\"2016-04-28T18:24:53.209+03:00\"},\"gvtAHogT3mWo1RBGBP5XwQ\":{\"token\":\"a$Mrj9itQxCipcnVwR6QzJi.m11QjMMIZ6I/iLcZFuUdntuYA.R0Xva\",\"expiry\":1463066717,\"last_token\":\"a$l2pwhjEEG/qgM28syM8jAuNfqg8y33aMHZh9AvuTiUlNJDfrF2OXu\",\"updated_at\":\"2016-04-28T18:25:17.418+03:00\"}}"], ["updated_at", 2016-04-28 17:29:27 UTC], ["id", 2]]
(0.5ms) COMMIT
Completed 500 Internal Server Error in 289ms (ActiveRecord: 1.0ms)
ActionView::MissingTemplate (Missing template overrides/sessions/destroy, devise_token_auth/sessions/destroy, devise_token_auth/application/destroy, devise/destroy, application/destroy with {:locale=>[:en], :formats=>["json"], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Users/dukei/Documents/Projects/YakTalk/app/views"
* "C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/devise_token_auth-0.1.37/app/views"
* "C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/devise-4.0.0.rc2/app/views"
):
app/controllers/overrides/sessions_controller.rb:123:in `render_destroy_success'
app/controllers/overrides/sessions_controller.rb:67:in `destroy'
routes.rb
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
sessions: 'overrides/sessions'
}
root 'application#angular'
resource :messages
end
问题出在哪里?
可能是 this issue 在 Rails 中使用 Jbuilder 5. 尝试渲染以下内容:
format.json {
render :plain => {success:true}.to_json, status: 200, content_type: 'application/json'
}
据我所知,json
可以在没有模板的情况下在 Rails 上呈现,使用如下代码:
module Overrides
class SessionsController < DeviseTokenAuth::SessionsController
def destroy
render json: {
errors: 'Test 200'
}, status: 200
end
我还尝试了以下操作:
respond_to do |format|
format.json { render :json => {success:true}, status: 200}
end
没有结果。我仍然遇到同样的错误。
Started DELETE "/auth/sign_out" for 127.0.0.1 at 2016-04-28 20:29:27 +0300
Processing by Overrides::SessionsController#destroy as json
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."uid" = LIMIT [["uid", "demo"], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (0.5ms) UPDATE "users" SET "tokens" = , "updated_at" = WHERE "users"."id" = [["tokens", "{\"xxxxx\":{\"token\":\"a$DvZoMNnonCc7QIPxUGgl2uVgY6ufM4dsdnD7PJxBkdDUJoY9cOXYS\",\"expiry\":1463064626,\"last_token\":\"a$bjFBVZanZhFRnh7D37/X/Or40I4IRSDP/fkzI3Uihucsa8/u.grNm\",\"updated_at\":\"2016-04-28T17:50:26.524+03:00\"},\"a85-6Pqg04JKNheGxUzaXQ\":{\"token\":\"a$Bmeo9N9CbUeU7S9nnGYLGukzNuBfx6aTlLjR5Lqp7XK0yNldCcVSe\",\"expiry\":1463064559},\"LQquXUPX8XYX2iT9cb5big\":{\"token\":\"aIEBKFHUmJH8stSg4erzl.7M/WersmemSgifGm97JAHPSM3QeIhVW\",\"expiry\":1463064626},\"o1ihjtFl80sXmVwRQpylFg\":{\"token\":\"a$FGLKBBipRM8KGUft6.KtQ.1C.4LEr3RV0nX3FsEwtSf3xNgIgBNsK\",\"expiry\":1463064845,\"last_token\":\"arKzo.BwG2eDYU/DUJiN/.D.jbfhlTUO1N1JyPJMtDoCUWZh.ToSa\",\"updated_at\":\"2016-04-28T17:54:05.714+03:00\"},\"default\":{\"token\":\"a$Q3Kpj2K9d.r22BavXNbFSu/swmKsjETvGzwiPe5OUW3OZ0Vx8MaX2\",\"expiry\":1463066712,\"last_token\":\"a$bNfn2noq6iIAelbrOsIZcum7z9aYtghtQzFu6.300zFaYM4N8BtAO\",\"updated_at\":\"2016-04-28T18:25:12.081+03:00\"},\"0Yb2s4qO8whrJftlZdJS3A\":{\"token\":\"a$kGj0h6VTRcb3.36jwvKU8eRcTElFjbY8WVofLOsRbzN8w2wYww0vi\",\"expiry\":1463066405,\"last_token\":\"a$dJso8ZQBJlL5KMPmoB5fROvZr5gIUEbiWP939L/DqGkDkjXSQ2xEC\",\"updated_at\":\"2016-04-28T18:20:05.611+03:00\"},\"3n866Hx7t4GXC8PasANnng\":{\"token\":\"a$uizgREN5xUQDrDutGNF9vu/v.obbj3O.2uF2WWLTeEcUaf51Aiore\",\"expiry\":1463066450,\"last_token\":\"a$rGw/ixMlnkA4Nn1p3qlQiORuMnxBcSZaOVGJaLWNqvG2HAh/uaxry\",\"updated_at\":\"2016-04-28T18:21:03.968+03:00\"},\"xqZiCAeG9qNttkIoH5wC9g\":{\"token\":\"a$XLu75dE44rv.avuesESY3evNLYUP9m.MnxBdFbgZ07ssFbPl1uLLu\",\"expiry\":1463066555,\"last_token\":\"a$v1z4lz/xPceYKo6jjLO7uej3fUTWNQ99Qb/2C8kK7hXPEvaNSoqm.\",\"updated_at\":\"2016-04-28T18:22:35.889+03:00\"},\"dbeTgN6KLQxWKhETjSjKbw\":{\"token\":\"a$o122yFjcZW7bZkyigKEUNeldwHBnlqALzqzvl11G3gCm50KWTkkuO\",\"expiry\":1463066693,\"last_token\":\"a$oHIZ87V4RNxBi6DF8pl2ReAXfct3m3E/Iuvbqe.hs8xeEH9.dxdBu\",\"updated_at\":\"2016-04-28T18:24:53.209+03:00\"},\"gvtAHogT3mWo1RBGBP5XwQ\":{\"token\":\"a$Mrj9itQxCipcnVwR6QzJi.m11QjMMIZ6I/iLcZFuUdntuYA.R0Xva\",\"expiry\":1463066717,\"last_token\":\"a$l2pwhjEEG/qgM28syM8jAuNfqg8y33aMHZh9AvuTiUlNJDfrF2OXu\",\"updated_at\":\"2016-04-28T18:25:17.418+03:00\"}}"], ["updated_at", 2016-04-28 17:29:27 UTC], ["id", 2]]
(0.5ms) COMMIT
Completed 500 Internal Server Error in 289ms (ActiveRecord: 1.0ms)
ActionView::MissingTemplate (Missing template overrides/sessions/destroy, devise_token_auth/sessions/destroy, devise_token_auth/application/destroy, devise/destroy, application/destroy with {:locale=>[:en], :formats=>["json"], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Users/dukei/Documents/Projects/YakTalk/app/views"
* "C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/devise_token_auth-0.1.37/app/views"
* "C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/devise-4.0.0.rc2/app/views"
):
app/controllers/overrides/sessions_controller.rb:123:in `render_destroy_success'
app/controllers/overrides/sessions_controller.rb:67:in `destroy'
routes.rb
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
sessions: 'overrides/sessions'
}
root 'application#angular'
resource :messages
end
问题出在哪里?
可能是 this issue 在 Rails 中使用 Jbuilder 5. 尝试渲染以下内容:
format.json {
render :plain => {success:true}.to_json, status: 200, content_type: 'application/json'
}