0

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?

3
  • 1
    Why do you want to render a template for json request? It's a json request, when you use render json: some_val inside some action then that action won't even render template for it. It'll simply output json data. Commented Jun 23, 2015 at 9:15
  • how to make the json presentable then so i can present the key value pairs i am interested in. Thats what Im asking Commented Jun 23, 2015 at 9:45
  • you can customize json response yourself like: 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 Commented Jun 23, 2015 at 10:30

1 Answer 1

1

If I am getting you issue correctly then you can do like this:

 def show
   @user = User.find_by name: params[:name]
   respond_to do |format|
     format.html { render :layout => false }
     format.json {  render json: @user.to_json } 
   end
 end

Now new can display user details on HTML page as:

<ul>
  <li id="Name">Name: <%= @user.name %></li>
  <li id=Email>Email: <%= @user.email %></li>
  <li id="id">Id: <%= @user.id %></li>
</ul>

Also in you graph you need to pass JSON response so there you can access JSON response as: Suppose you have to pass a data in you graph so just do it as

 var data = "users/:id.json" #call your show action in json format
Sign up to request clarification or add additional context in comments.

3 Comments

but if I do format.html and then format.json it just gives me the html rather than the json response?
yes you are right, if I am not wrong you are using json response inside your java script, right?
So you can use json response in your javascript as "users/:id.json"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.