Viktor Danilov Viktor Danilov - 7 months ago 29
Ruby Question

Render :json without a template

As I know,

json
can be rendered on Rails without a template, using such code as:

module Overrides
class SessionsController < DeviseTokenAuth::SessionsController
def destroy
render json: {
errors: 'Test 200'
}, status: 200
end


Also I tried following:

respond_to do |format|
format.json { render :json => {success:true}, status: 200}
end


No results. I'm still getting the same error.

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" = $1 LIMIT $2 [["uid", "demo"], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (0.5ms) UPDATE "users" SET "tokens" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["tokens", "{\"xxxxx\":{\"token\":\"$2a$10$DvZoMNnonCc7QIPxUGgl2uVgY6ufM4dsdnD7PJxBkdDUJoY9cOXYS\",\"expiry\":1463064626,\"last_token\":\"$2a$10$bjFBVZanZhFRnh7D37/X/Or40I4IRSDP/fkzI3Uihucsa8/u.grNm\",\"updated_at\":\"2016-04-28T17:50:26.524+03:00\"},\"a85-6Pqg04JKNheGxUzaXQ\":{\"token\":\"$2a$10$Bmeo9N9CbUeU7S9nnGYLGukzNuBfx6aTlLjR5Lqp7XK0yNldCcVSe\",\"expiry\":1463064559},\"LQquXUPX8XYX2iT9cb5big\":{\"token\":\"$2a$10$3IEBKFHUmJH8stSg4erzl.7M/WersmemSgifGm97JAHPSM3QeIhVW\",\"expiry\":1463064626},\"o1ihjtFl80sXmVwRQpylFg\":{\"token\":\"$2a$10$FGLKBBipRM8KGUft6.KtQ.1C.4LEr3RV0nX3FsEwtSf3xNgIgBNsK\",\"expiry\":1463064845,\"last_token\":\"$2a$10$4rKzo.BwG2eDYU/DUJiN/.D.jbfhlTUO1N1JyPJMtDoCUWZh.ToSa\",\"updated_at\":\"2016-04-28T17:54:05.714+03:00\"},\"default\":{\"token\":\"$2a$10$Q3Kpj2K9d.r22BavXNbFSu/swmKsjETvGzwiPe5OUW3OZ0Vx8MaX2\",\"expiry\":1463066712,\"last_token\":\"$2a$10$bNfn2noq6iIAelbrOsIZcum7z9aYtghtQzFu6.300zFaYM4N8BtAO\",\"updated_at\":\"2016-04-28T18:25:12.081+03:00\"},\"0Yb2s4qO8whrJftlZdJS3A\":{\"token\":\"$2a$10$kGj0h6VTRcb3.36jwvKU8eRcTElFjbY8WVofLOsRbzN8w2wYww0vi\",\"expiry\":1463066405,\"last_token\":\"$2a$10$dJso8ZQBJlL5KMPmoB5fROvZr5gIUEbiWP939L/DqGkDkjXSQ2xEC\",\"updated_at\":\"2016-04-28T18:20:05.611+03:00\"},\"3n866Hx7t4GXC8PasANnng\":{\"token\":\"$2a$10$uizgREN5xUQDrDutGNF9vu/v.obbj3O.2uF2WWLTeEcUaf51Aiore\",\"expiry\":1463066450,\"last_token\":\"$2a$10$rGw/ixMlnkA4Nn1p3qlQiORuMnxBcSZaOVGJaLWNqvG2HAh/uaxry\",\"updated_at\":\"2016-04-28T18:21:03.968+03:00\"},\"xqZiCAeG9qNttkIoH5wC9g\":{\"token\":\"$2a$10$XLu75dE44rv.avuesESY3evNLYUP9m.MnxBdFbgZ07ssFbPl1uLLu\",\"expiry\":1463066555,\"last_token\":\"$2a$10$v1z4lz/xPceYKo6jjLO7uej3fUTWNQ99Qb/2C8kK7hXPEvaNSoqm.\",\"updated_at\":\"2016-04-28T18:22:35.889+03:00\"},\"dbeTgN6KLQxWKhETjSjKbw\":{\"token\":\"$2a$10$o122yFjcZW7bZkyigKEUNeldwHBnlqALzqzvl11G3gCm50KWTkkuO\",\"expiry\":1463066693,\"last_token\":\"$2a$10$oHIZ87V4RNxBi6DF8pl2ReAXfct3m3E/Iuvbqe.hs8xeEH9.dxdBu\",\"updated_at\":\"2016-04-28T18:24:53.209+03:00\"},\"gvtAHogT3mWo1RBGBP5XwQ\":{\"token\":\"$2a$10$Mrj9itQxCipcnVwR6QzJi.m11QjMMIZ6I/iLcZFuUdntuYA.R0Xva\",\"expiry\":1463066717,\"last_token\":\"$2a$10$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


Where lies the problem?

Answer

It's possible that it's this issue with Jbuilder in Rails 5. Try rendering the following:

format.json { 
  render :plain => {success:true}.to_json, status: 200, content_type: 'application/json'
}