# Call action from an action

**URL:** <https://www.ruby-forum.com/t/call-action-from-an-action/88329>\
**Category:** Rails\
**Created:** [March 9, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329 "2007-03-09T20:06:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/1 "2007-03-09T20:06:39Z")

</div>

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?

Thanks,

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/2 "2007-03-09T20:15:54Z")

</div>

You could call ‘index’ like a method from the other action, but then  
you’ll still need to do the render in the other action. So…

def archive  
#setup instance vars  
index #go run the code in ‘index’  
render :action =\> ‘index’ #render the template  
end

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/3 "2007-03-09T20:15:54Z")

</div>

> I would like to have another action that sets up a few instance vars and  
> 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

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/4 "2007-03-09T20:20:47Z")

</div>

Jon G. wrote:

> You could call ‘index’ like a method from the other action, but then  
> you’ll still need to do the render in the other action. So…
> 
> def archive  
> #setup instance vars  
> index #go run the code in ‘index’  
> render :action =\> ‘index’ #render the template  
> end

That’s what I thought would work, but I tried that and for some reason  
it gives me:

Template is missing  
missing template /user/admin/stuff/config/…/etc/etc/archived.rhtml

So it’s still trying to get to the archived view.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/5 "2007-03-09T20:27:18Z")

</div>

Philip H. 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!

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/6 "2007-03-09T20:40:12Z")

</div>

Bill W. wrote:

> Hi David,
> 
> David C. wrote:
> 
> > > def archive  
> > > missing template /user/admin/stuff/config/…/etc/etc/archived.rhtml
> 
> Is that a typo? archive vs. archived?
> 
> Bill

Yeah, typo. It’s still looking for the archive template, although I’ve  
called render on the index action.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/7 "2007-03-09T20:29:01Z")

</div>

Hi David,

David C. wrote:

> > def archive  
> > missing template /user/admin/stuff/config/…/etc/etc/archived.rhtml

Is that a typo? archive vs. archived?

Bill

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/call-action-from-an-action/88329/8 "2007-03-09T22:25:18Z")

</div>

> 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 🙂
