I have a problem with reading JSON on a webpage. My webpage consists of a bunch of cirlces and every time someone presses on a circle a call to the controller is made to query a database and return information about that particular circle. The information is presented as various graphs as well as text information.
The controller returns JSON because that is what the graph needs. However I also want to present text information. At present the text information is presented as unformatted JSON whereas I want to display only certain parts of it
The controller:
def show
@user = User.find_by name: params[:name]
respond_to do |format|
# Here we need to remove the layout because the request
# is done via ajax and the layout is already loaded.
#format.html { render :layout => false }
format.json { render json: @user.to_json }
end
end
the page (graph not shown)
<ul>
<li id="Name">Name: <%= @user.name %></li>
<li id=Email>Email: <%= @user.email %></li>
<li id="id">Id: <%= @user.id %></li>
<li id="FBScore">Facebook Score: <%= @user.FBScore %></li>
<li id="PinterestScore">Pinterest Score: <%= @user.PinterestScore %></li>
<li id="InstagramScore">Instagram Score: <%= @user.InstagramScore %></li>
<li id="TwitterScore">Twitter Score: <%= @user.TwitterScore %></li>
<h1><li id="OverallScore">Overall Score:<%= @user.TwitterScore + @user.InstagramScore + @user.PinterestScore + @user.FBScore %></h1>
</ul>
the current result:
Profile details
Score details
{"id":372,"name":"Voluptatem Quo Facilis","email":null,"created_at":"2015-06-22T10:46:59.827Z","updated_at":"2015-06-22T10:46:59.827Z","password_digest":null,"remember_digest":null,"admin":null,"activation_digest":null,"activated":null,"activated_at":null,"reset_digest":null,"reset_sent_at":null,"group":4,"FScore":27,"TScore":48,"IScore":94,"PScore":93}
What I want:
Profile details
Score details
id:372
name:Voluptatem Quo Facilis
email: null
FScore:27
TScore:48
IScore:94
PScore:93
So I suppose the question relies on how do I write the variables so that I can extract the bits of JSON I want to display?
render json: some_valinside some action then that action won't even render template for it. It'll simply output json data.render json: {some_key: some_value, some_other_key: {some_key: some_value}}or you can look at active_model_serializers or some other gem for customizing json data