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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:55:02+00:00 2026-05-19T16:55:02+00:00

I have a bunch of controllers with methods that render the index view. This

  • 0

I have a bunch of controllers with methods that render the index view. This results in me writing render :index at the end of the majority of methods. Here’s an example

def index
  @models = Model.find(:all)
end

def new_models
  @models = Model.find_by_new(true)

  render :index
end

def old_models
  @models = Model.find_by_new(false)

  render :index
end

Ideally, I would just move the render code into an after filter, but as the controller makes a call to render before going to the after filter that’s not an option.

I have a large number of controllers like this so a solution would remove a lot of repeated code.

This app is currently still Rails 2.3, however, it will be upgraded to Rails 3 in the next month or two. So while I would prefer a technique that works on 2.3, Rails 3 only solutions would still be appreciated.

  • 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-19T16:55:03+00:00Added an answer on May 19, 2026 at 4:55 pm

    If you really want to DRY up this action, and if it’s very common, then you can do some meta-programming of your own. First create a render_with_index.rb file with this module definition:

    module RenderWithIndex
      def self.included klass
        klass.class_eval do
          def self.render_with_index * methods
            methods.each do |method|
              self.class_eval <<-EVAL
                alias :old_method :#{method}
    
                def #{method}
                  old_method
                  render :index
                end
              EVAL
            end
          end
        end
      end
    end
    

    Then include that module in your controller and define the methods that should render with index (make sure the render_with_index call happens after your method declarations.

    include RenderWithIndex
    
    def index
      @models = Model.find(:all)
    end
    
    def new_models
      @models = Model.find_by_new(true)
    end
    
    def old_models
      @models = Model.find_by_new(false)
    end
    
    render_with_index :new_models, :old_models
    

    This module now allows you to render any action with the index template simply by adding it to the render_with_index call.

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

Sidebar

Related Questions

I have bunch of lists like this: out[3] [[3]] [1] forum=28&mid=11883 [2] forum=29&mid=11884 out[4]
I have a bunch of html files that come with my app and are
I have a bunch of code in a routine that looks a bit like
I have a bunch of strings that are dependent on static dictionaries and each
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I'm working with ASP.NET MVC 2. I have a bunch of partial views that
I have a bunch of Rails 3.1 controllers which all have very similar testing
We have a bunch of pages that get really high traffic, and as such
I have a bunch of controllers in a namespace Foo . I'd like to
I have a controller setup that uses a whole bunch of different AjaxContent helpers.

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.