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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:41+00:00 2026-05-28T22:09:41+00:00

I an new in ruby and rails. Following the guide in ror document,I create

  • 0

I an new in ruby and rails.

Following the guide in ror document,I create the blog application.

Howver when I see the code generated,I found that I can not understand them,for exmpale:

  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

The repond_to is a method(isn’t it?),and the following block is the argument?

However what does the code inner the block mean?

      format.html # show.html.erb
      format.json { render json: @post }

Is the format.html is the name of the method or something else?

ANd how about the { render json: @post }>?

  • 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-28T22:09:41+00:00Added an answer on May 28, 2026 at 10:09 pm

    The respond_to method helps you to deliver the content in the format requested. For example, if you call /posts/1.json, the response would be a JSON file. If it’s /posts/1.html, the response would be an HTML page. The default when no extension is provided is to render HTML.

    The format.json method tells Rails what to do when requested that extension, like for example, if for every JSON request you would like to increase a counter, but not for HTML requests, you could do:

    format.json { 
        counter = counter + 1
        render json: @post
    }
    

    If you don’t provide a block to the format.json method, Rails automagically will try and look inside views/posts/ for a show.json.erb file, and render that. In the method you provided, render json: @post tells Rails to render it immediately instead of looking for a file.

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

Sidebar

Related Questions

I am new to Ruby on Rails and am following a tutorial to create
I am new to Ruby on Rails, I have completed the Blog Tutorial .
I'm new to Ruby on Rails, and I'm trying to create a bass guitar
I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy
I'm very new to Ruby on Rails, and have been getting the following error
Im new to ruby on rails and i have been following The Rails Space
I am using cucumber to write features for a new ruby on rails application.
I am quite new to Ruby and Ruby on Rails. While following a Ruby
I'm new at ruby on rails, and I wanted to accomplish the following: I've
I'm new to Ruby on Rails, and I'm setting up a dev environment following

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.