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 9158287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:12:40+00:00 2026-06-17T13:12:40+00:00

I used scaffolding to create a CRUD system for posts. In the controller, I

  • 0

I used scaffolding to create a CRUD system for posts. In the controller, I see this:

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
  • What is respond_to and where does it come from? Since it is using the do operator, it is some sort of iterable list, I’m assuming. On each format in that list, it will perform the html and json methods.

  • How does { render json: @posts } work in relation to the json method? Is render json: @posts being passed as an argument to the method? Is render and json each an object? I’ve never seen the colon notation used outside of symbols.

  • 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-06-17T13:12:41+00:00Added an answer on June 17, 2026 at 1:12 pm
    1. PostsController inherits methods from ApplicationController and ApplicationController inherits from ActionController::Base. That’s where responds_to comes from. The subject there worth looking into is “method lookup”.
    2. do ... end is one way of writing a block.
      { render json: @posts } is another way.
    3. json: "foo" is a more modern alternative to writing :json => "foo"
    4. format is an arbitrary variable you’re cooking up to use inside the block. render is a method and :json is a symbol. respond_to will respond to user requests that Rails format responses accordingly.
    5. And for understanding the method, there’s also this:

    http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

    And if you want to look at the source (it’s a little thick), for example at the respond_with method that Paul mentioned, that’s in the Rails source here:

    rails/actionpack/lib/action_controller/metal/mime_responds.rb

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used nifty scaffolding for create controller 'A'. For this controller I have in
Since I used scaffolding to create my Deposit model, resources :deposits generates a bunch
I am using rails 3 on windows. I used scaffolding to create a party
I used the phonegap website to create an IPA file. Now I want to
i used this code: List<string> lists=new List<string>(apple,orange,banana,apple,mang0,orange); string names; names=lists.Distinct() is that correct?
Trying to add a Controller with scaffolding and getting an error. Seems to have
Used this StackOverflow answer to try an implement a submitless form. The page consists
Used this tutorial I have installed jogl, jocl, gluegen, joal libs and added its
I'm writing a rails app about videogame systems. I used scaffolding to generate my
I've been trying to get my own opinion about this issue but could not,

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.