Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:32:31+00:00 2026-05-23T02:32:31+00:00

I am working on ruby on rails project and I want to add respond

  • 0

I am working on ruby on rails project and I want to add respond to Json.

One simple way is:–

def index
  @users = User.all
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @users }
    format.json  { render :json => @users.to_json }
  end
end

But there are some issues with this:-

  1. I don’t want to give the whole user object in json response like password hash and cache counter attributes. Facebook, twitter attributes etc.
  2. I want to add more details in the json object (considering stackoverflow model) like Latest question by each user, latest answer given by each user. Instead of image name stored in db via paperclip, I want to pass full url of the image.

So the question is how can code json reponse in index.json.erb file like we do in index.html.erb. Format your own json response as per needs.

EDIT

def index
  @users = User.all
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @users }
    format.json  { render :file => "index.json.erb", :content_type => 'application/json' }
  end
end

index.json.erb file:-

    <% @users.each do |user| %>
    {
      first_name: <%= user.first_name %>,
      last_name: <%= user.last_name %>  
    }

    <% end %>

Error:–

template missing.

PS:– I am just trying using creating this file. This is just a sample

EDIT

enter image description here

edit

 { { first_name: Mohit , last_name: Jain } { first_name: Sahil Miglani, last_name: } { first_name: Hitesh Bansal, last_name: } { first_name: Sudhanshu, last_name: } { first_name: Saakshi, last_name: } { first_name: Kutta, last_name: } { first_name: bc, last_name: } { first_name: hey, last_name: } { first_name: fewhjfbwfb, last_name: vdsv } } 

EDIT

    [ { first_name: Mohit , last_name: Jain } , { first_name: Sahil Miglani, last_name: } , { first_name: Hitesh Bansal, last_name: } , { first_name: Sudhanshu, last_name: } , { first_name: Saakshi, last_name: } , { first_name: Kutta, last_name: } , { first_name: bc, last_name: } , { first_name: hey, last_name: } , { first_name: fewhjfbwfb, last_name: vdsv } ] 

EDIT, quite close to find out the solution:-

    [
    <% @users.each_with_index do |user,index| %>
    {
        <%= "first_name".to_json.html_safe %>: <%= user.first_name.to_json.html_safe %>,
        <%= "last_name".to_json.html_safe %>: <%= user.last_name.to_json.html_safe %>  
    }
    <% unless index== @users.count - 1%>
    ,
    <% end %>
    <% end %>
    ]

Response:-

    [

       -
       {
           first_name: "Mohit "
           last_name: "Jain"
       }
       -
       {
           first_name: "Sahil Miglani"
           last_name: null
       }
       -
       {
           first_name: "Hitesh Bansal"
           last_name: null
       }
       -
       {
           first_name: "Sudhanshu"
           last_name: null
       }
       -
       {
           first_name: "Saakshi"
           last_name: null
       }
       -
       {
           first_name: "Kutta"
           last_name: null
       }
       -
       {
           first_name: "bc"
           last_name: null
       }
       -
       {
           first_name: "hey"
           last_name: null
       }
       -
       {
           first_name: "fewhjfbwfb"
           last_name: "vdsv"
       }

    ]

Now all i want is to enclose this each response section in user array

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:32:32+00:00Added an answer on May 23, 2026 at 2:32 am

    You can render a json.erb file and use it like a normal template:

    # file: app/views/your_controller/your_action.json.erb
    {
      filed1: <%= @some_var %>,
      field2: <%= @another_var %>,
      fieldN: <%= @yet_another_var %>,
      data: <%= @some_data.to_json.html_safe %>
    }
    

    in your controller, call the explicit render with content_type:

    render :file => "your_file.json.erb", :content_type => 'application/json'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a ruby on rails project that requires recording some simple user
I am working on an existing Ruby on Rails project. While one of the
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
im working in a project writen in ruby on rails and im currently using
I have the simple following code, which is working in a ruby (not rails)
I'm doing my first Ruby on Rails project. I am working on a project
I'm working on a Ruby on Rails project and sometimes I need to run
This is the error I'm getting: Task not supported by 'mysql2' /Users/me/.rvm/gems/ruby-1.8.7-p352@project/gems/rails-2.3.8/lib/tasks/databases.rake:380 Here is
I am new to Rails / Ruby. I am working on a project where
I am working on ruby rails project. I am using Rails 2.3.4 and ruby

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.