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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:55:59+00:00 2026-05-26T00:55:59+00:00

I have created some modules using scaffold, but I can’t understand how can we

  • 0

I have created some modules using scaffold, but I can’t understand how can we use respond_to without relating to an object or anything?

respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :ok }
end

I have researched about the ‘do’ and found that it is like ‘each’ deals with enumerable object

  • 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-26T00:56:00+00:00Added an answer on May 26, 2026 at 12:56 am

    the part between do and end is a ruby block. If you’re familiar with anonymous functions, it shares some similitudes with this concept. respond_to is an instance method on your controller ; the block is passed to this method. |format| is the argument that this method passes to the block at some point of its execution.

    the good thing with blocks is that they are evaluated in the context (“binding”) of the method caller, in this case the instance of your controller. So you can use any variables or methods you could use in your controller inside your block – even if you were calling a method on another object. This is a very powerful feature from Ruby, and as @Niklaos and @Perry said, you should definitely learn more about ruby idioms because these blocks are everywhere !

    the other syntax for a block is

    # the number of yielded arguments can vary from zero to many
    method {|yielded_argument| do_something_with yielded_argument} 
    

    One more thing : many iterator methods use blocks in ruby, but blocks are not limited to iteration. ‘each’ for example is an iterator that yields (passes to the block) every member of a collection from which it is called on, one after another.

    So, to answer your question the respond_to method yields an ActionController::MimeResponds::Collector object to the block ; in this block you configure different responses for different MIME types. To do so you pass another block to one or many of the MIME methods (html,json,etc.) of this object.

    The controller then uses this Collector object to determine the appropriate response to a request (render an html.erb template, or format content as json, etc. – note that redirect_to, for example, is indeed a controller instance method). A good documentation for respond_to can be found here.
    Hope this helped !

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

Sidebar

Related Questions

I have created some rounded navigation tabs using CSS and am having trouble when
I have created some simple app in Java, and 'deployed' it using Java Web
I have created some python code which creates an object in a loop, and
I have created some functional categories that I use to show/hide markup elements. However,
I'm using SQL*Plus 9.2 on Oracle 10g enterprise. I have created some scripts that
I m new to use ankhSVN and having issues. I have created some new
I have created a custom module and am using hook_block to programmatically create some
I'm using ASP.NET with C# 2.0. I have created some objects for a database
I have created some extra functionality on my Linq-to-SQL classes to make things easier
I have created some tests in my WPF application. Right now I am working

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.