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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:24:02+00:00 2026-05-14T19:24:02+00:00

I often use respond_to do |format| … end in Rails for my Restful actions,

  • 0

I often use

respond_to do |format|
...
end

in Rails for my Restful actions, but I don’t know what the ideal solution is for handling various forms of, say, html responses. For instance, view1 that calls action A might expect back html with a list of widgets wrapped in a UL tag, while view2 expects the same list of widgets wrapped in a table. How does one Restfully express that not only do I want back an html formatted response, but I want it wrapped in a table, or in a UL, OL, options, or some other common list-oriented html tag?

  • 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-14T19:24:03+00:00Added an answer on May 14, 2026 at 7:24 pm

    This is the basic idea:

    Controller

    class ProductsController < ApplicationController
    
      def index
    
        # this will be used in the view
        @mode = params[:mode] || 'list'
    
        # respond_to is used for responding to different formats
        respond_to do |format|
          format.html            # index.html.erb
          format.js              # index.js.erb
          format.xml do          # index.xml.erb
            # custom things can go in a block like this
          end
        end
      end
    
    end
    

    Views

    <!-- views/products/index.html.erb -->
    <h1>Listing Products</h1>
    
    <%= render params[:mode], :products => @products %>
    
    
    <!-- views/products/_list.html.erb -->
    <ul>
      <% for p in products %>
      <li><%= p.name %></li>
      <% end %>
    </ul>
    
    
    <!-- views/products/_table.html.erb -->
    <table>
      <% for p in products %>
      <tr>
        <td><%= p.name %></td>
      </tr>
      <% end %>
    </table>
    

    Usage:

    You can link to other views “modes” using:

    <%= link_to "View as list",   products_path(:mode => "list") %>
    <%= link_to "View as table",  products_path(:mode => "table") %> 
    

    Note: You’ll want to do something to ensure that the user doesn’t attempt to specify an invalid view mode in the URL.

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

Sidebar

Related Questions

For all the RSS feeds I subscribe to I use Google Reader , which
I've optimized my code to use only lightweight libraries. I'm even using the low
I'm new to U/I programming, and I'm trying to get started with OpenGL. When
Our application has ~10 threads doing separate tasks (no thread pools). We are not
I have an application that uses MSMQ for asynchronous processing of certain things. I
Now there is a subject that could be taken many ways. Hopefully I will
I ask this academically, I want to ask aloud a very important question and
In Inform, I'd like to be able to create a new action, and have
I've noticed many questions on here from new programmers that can be solved using

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.