# How can I call another controller's action (or better yet, h

**URL:** <https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793>\
**Category:** Rails\
**Created:** [July 7, 2007, 4:17am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793 "2007-07-07T04:17:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 7, 2007, 4:17am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/1 "2007-07-07T04:17:02Z")

</div>

I’ve got an action that runs, and when it’s done I want the user to  
POST to another URL. However I don’t want to display a form that they  
need to submit.

The two ideas I’ve had are to either call OtherController#create  
directly, or to send some response to the browser that initiates a  
POST back to the correct URL.

Anyone know how to do this, or have any other ideas?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 7, 2007, 5:03am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/2 "2007-07-07T05:03:05Z")

</div>

Hi Pat,

Pat M. wrote:

> I’ve got an action that runs, and when it’s done  
> I want the user to POST to another URL.

I haven’t tested it with the POST, but maybe just…

redirect\_to :controller =\> controller\_name, :action =\> action\_name,  
:method  
=\> “post”

If redirect\_to won’t take the POST, you could maybe query  
controller.controller\_name and/or controller.action\_name to see where  
the  
request came from and then treat it accordingly.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 7, 2007, 6:32am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/3 "2007-07-07T06:32:09Z")

</div>

On Jul 6, 8:15 pm, “Pat M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pat  
> check out

[http://drawohara.tumblr.com/post/2070878](http://drawohara.tumblr.com/post/2070878)

-a

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 8, 2007, 12:09am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/4 "2007-07-08T00:09:39Z")

</div>

AFAIK, HTTP doesn’t allow redirects using the POST method… which  
makes sense. A redirect is either a “302 Found” or a “301 Moved  
Permanently”, usually the former. Either of these are saying “you  
asked for a resource, but it’s not here… _here_ (the Location  
header) is where to get it.” Nothing about form submission. Redirects  
are always GETs.

If you want the form data to be processed as a POST in a different  
controller and originating from the client, I think javascript is  
about your only option. You can use the rails prototype library to  
send a post from within javascript and without showing a form to the  
user. I’ve done this many times.

However, I’m not sure that that’s what you really want. What is it  
that the first action is doing? Was it handling a form submission? If  
the problem is that you have some logic in one action that you need in  
two places, then you could pull that out into a helper method in  
application\_helper. You can also do common side-processing in  
filters.

I haven’t done your idea of calling an action from within another  
action, but that might work. Just remember that is a regular method  
call, so control will return to the caller after the called action is  
done. And you can only call render in there once, so you’d need  
conditionals around the render calls if there’s more than one.

Well, I hope something in there helps…

Ben

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 9, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/5 "2007-07-09T20:37:36Z")

</div>

why not just call the create method and return when the user doesn’t  
need to see the preview?

def final\_step  
…  
create() and return  
…  
end

the whole js onload thing seems a bit gnarly!

worse case… to DRY things up… I would say just put the logic in a  
shared module and call it from your controller methods?

good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 9, 2007, 10:06am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/6 "2007-07-09T10:06:30Z")

</div>

The user goes through a workflow, updating a db record in a series of  
steps. When the whole thing is done, we create another record (of a  
separate class).

The problem is that sometimes they have a final Preview step and  
sometimes they don’t. When the user has a preview step, I show a  
filled in form. They submit it, our record is created, all is good.  
When they don’t have a preview step though, we never see this page.  
So I have to handle it before that, when they complete the final step  
of the workflow.

So basically they same logic goes in the VideosController#create  
method and the final step of the workflow. That sucks.

I think a _reasonable_ approach is, if the user doesn’t have a preview  
step, redirect them to a blank page that has the form in it. Use  
page.onload to automatically submit the form. This way I keep all the  
final creation logic in one place.

Thoughts?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 10, 2007, 2:17am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/7 "2007-07-10T02:17:13Z")

</div>

I need to run a couple more statements than just the create. It’s  
creating associated objects, but I don’t think it should be done as  
part of a create hook. There’s only like three lines that need to be  
duplicated. So I guess I’ll move it into a module.

I think I might be missing something though. So let me give a bit more  
detail.

class Video \< AR  
has\_many :assets  
end

class Asset \< AR; end

The create is going to be a simple  
@video = Video.create params[:video]

From here I need to create a couple assets. I _could_ just do this in  
a create hook, except for the fact that I need to pass them a frame  
number. This never gets stored anywhere but gets sent back to a  
transcoding server.

So I feel weird about putting a frame\_number accessor on either Video  
or Asset. They’d only ever use it once, and it just seems like it  
shouldn’t go there.

Doing this all in a create hook would also make testing tougher  
because I’d have to stub lots of stuff.

I _think_ what I should do is write a new builder/method or class that  
handles all of this. I can call that method, passing it all the info  
I need, and it will create the video, the assets, and tell the other  
server about it. That way I can encapsulate the entire building  
process by one method, so I don’t have any duplication, and my AR  
classes stay lightweight and don’t have to worry about constructing  
all these other things.

Sorry if that’s a bit rambly, it’s basically me talking things  
through. Thanks for listening 🙂

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [July 10, 2007, 5:56am UTC](https://www.ruby-forum.com/t/how-can-i-call-another-controllers-action-or-better-yet-h/108793/8 "2007-07-10T05:56:29Z")

</div>

I’d do this in the Video model, even if it touches things (like  
frame\_number) that don’t get persisted to a database, it sounds like a  
create\_with\_assets method that does what you say will just implement  
your “business rules” where a video needs to be created together with  
those assests under some circustamces (and just having a “fat” model  
is not bad if it helps to better define the domain model and its  
associated rules)

just my 2c.

Luca M.

2007/7/10, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
