I'm using Gon gem to send some data from Rails to BackBone.
My application_controller.rb
:
class ApplicationController < ActionController::Base
before_action :init_gon
def init_gon
gon.me = render_to_string(template: 'users/_me.json.jbuilder')
end
end
So after this, if I open any page of my web application, it only displays html as plain text:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="" xmlns="http://www.w3.org/1999/xhtml">
<head>
.....
How can I handle this?
I think your problem is to do with your rendering gon
data as string. I think you should pass your data without the need to render a string
I don't have any experience of that particular part, but I have used gon
extensively before. Here's a snippet of a RailsCast covering the subject:
Gon simply creates a script tag then fills a gon variable with the data that we set in the controller. If we want to customize the JSON that’s returned to the client Gon has support for both RABL and Jbuilder templates, both of which have been covered in recent episodes. To customize the data returned by the list of products we can create an index.json.rabl file and define the attributes we want to be returned there:
/app/views/products/index.json.rabl
collection Product.limit(10)
attributes :id, :name, :price
/app/controllers/products_controller.rb
class ProductsController < ApplicationController
def index
gon.rabl "app/views/products/index.json.rabl", as: "products"
end
end
JBuilder
Perhaps this will help? There is a way to render GON data with jBuilder (as I see you are using):
#app/controllers/application_controller.rb
def init_gon
gon.jbuilder template: 'users/_me.json.jbuilder'
end