# New controller - how do I access it via other controllers/views?

**URL:** <https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878>\
**Category:** Rails\
**Created:** [May 19, 2010, 8:08pm UTC](https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878 "2010-05-19T20:08:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 19, 2010, 8:08pm UTC](https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878/1 "2010-05-19T20:08:21Z")

</div>

Guys,

I have created a new controller Worker which has a method  
do\_work(inputs), and another method show\_work, which has a meaningful  
associated view (showing the results of the ‘work’.

So, I need to display the result of show\_work in the view of another  
class called Consumer.

So, I need to do a number of things:

1.) setup inputs (a hash) in the Consumer controller.  
2.) have the consumer controller show method call Worker’s method  
do\_work(inputs)  
3.) have the consumer show view show the results of show\_work

So, 2.) how do you call Worker.do\_work(inputs)? Do I instantiate a  
Worker class and then call obj\_name.do\_work(inputs)?  
3.) can I make use of the rendering going on in the show\_work view in  
another classes show view?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 19, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878/2 "2010-05-19T20:22:25Z")

</div>

On Wed, May 19, 2010 at 11:07 AM, chewmanfoo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1.) setup inputs (a hash) in the Consumer controller.  
> 2.) have the consumer controller show method call Worker’s method  
> do\_work(inputs)  
> 3.) have the consumer show view show the results of show\_work
> 
> So, 2.) how do you call Worker.do\_work(inputs)? Do I instantiate a  
> Worker class and then call obj\_name.do\_work(inputs)?

You can create a class method on the Worker’s model. For example,

def self.do\_work( inputs )  
…  
end

> 3.) can I make use of the rendering going on in the show\_work view in  
> another classes show view?

You can create a shared partial that can be rendered within the show  
template of the different controllers. Also, you can find alot of  
information  
on [guides.rubyonrails.org](http://guides.rubyonrails.org)

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 19, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878/3 "2010-05-19T20:27:41Z")

</div>

regarding the class method you mentioned, then I can run the  
Worker.do\_work(inputs) method by calling Worker.do\_work(inputs)?

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 19, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/new-controller-how-do-i-access-it-via-other-controllers-views/188878/4 "2010-05-19T21:09:50Z")

</div>

What chewmanfoo is talking about is moving code from the controller  
into the model. Basically, the consumer controller would call the  
worker model (if one exists), not the worker controller. When you’re  
in a situation where you need to run two or more controller actions on  
the same request then it’s a sign that your implementation isn’t  
optimal. To call two controller actions you need two requests, which  
means a redirect after the first one, which means you need some place  
to store the state between the events. It shouldn’t be necessary.
