call action from an action

I have an index action that shows a list of data.

   def index      # go get data,      # setup some instance vars      # render view - display data in table    end

   URL: /orders/

I would like to have another action that sets up a few instance vars and then calls the index action, which then takes over from there and goes and gets data, displays the data in a table.

  archive > sets up vars > runs index action code > renders index action view

I was assuming that I could just do:

   def archive      #setup instance vars      render :action => "index"    end

   URL: /orders/;archive

But this just renders the index actions view, and doesn't actually execute the index action code.

Just wondering if it's possible to have an action do a bit of setup, then hand things over to another action?

If it were me, I'd separate that common functionality out into a protected method and have both index and archive call it directly or via a before_filter.

This way when you realize that you need to do things a little different b/n index and archive it's a bit easier and clearer as to what you're affecting.

-philip

Philip Hallstrom wrote:

If it were me, I'd separate that common functionality out into a protected method and have both index and archive call it directly or via a before_filter.

Good deal. I created a private "listme" action and stuck the common code in there.

For some reason, now when I call:

def archived   #make some instance vars   listme   render :action => 'index' end

I don't get the missing template error. Totally strange.

Any clue as to why I was getting the template error (see post above).

Thanks for the suggestion - learn something new every day!

I believe when you call the the index action from archived index does an implicit "render..." and uses the current action name for the template... which is archived.

I suspect if you created a blank "archived.rhtml" you'd then get a double rendering error.

But I could be totally wrong on both accounts as I haven't really looked into it :slight_smile: