# Running commands from context of a view

**URL:** <https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894>\
**Category:** Rails\
**Created:** [November 12, 2007, 1:00am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894 "2007-11-12T01:00:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 12, 2007, 1:00am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/1 "2007-11-12T01:00:33Z")

</div>

From the command line, Is it possible to run a particular method in a  
view from a specified controller.

e.g.

class MyController \< ActionController  
def test  
end  
end

view.rhtml  
\<%= link\_to :action =\> :test %\>

rendered\_html

[ 

I’d like to be able to get that from the command line:  
StudentProfilesController.new.send(‘link\_to’, :action =\> test)

 

There are also a large number of other view commands i’d like to able to  
call, so just grabbing out link\_to doesn’t solve the issue

 

Cheers!

](https://www.ruby-forum.com/my/test)

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 12, 2007, 1:01am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/2 "2007-11-12T01:01:33Z")

</div>

Sorry:  
StudentProfilesController.new.send(‘link\_to’, :action =\> test)

was supposed to be:  
MyController.new.send(‘link\_to’, :action =\> test)

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 12, 2007, 1:34am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/3 "2007-11-12T01:34:52Z")

</div>

i’ve tried:

a = MyController.new.view\_class.new  
a.send(‘initialize\_current\_url’)  
vx = MyController.view\_class.new(StudentProfilesController.view\_root,  
{}, a)  
vx.link\_to(:controller =\> ‘test’, :action =\> ‘test’)

but because half the internals are not initialized it doesn’t work.

Would trying to simulate the request and response parameters?  
ActionController::Base  
def process(request, response, method = :perform\_action, \*arguments)  
#:nodoc:

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 12, 2007, 11:16am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/4 "2007-11-12T11:16:20Z")

</div>

On 12 Nov 2007, at 00:34, Alex M. wrote:

> i’ve tried:
> 
> a = MyController.new.view\_class.new  
> a.send(‘initialize\_current\_url’)  
> vx = MyController.view\_class.new(StudentProfilesController.view\_root,  
> {}, a)  
> vx.link\_to(:controller =\> ‘test’, :action =\> ‘test’)
> 
> but because half the internals are not initialized it doesn’t work.

Have you seen this: [http://snippets.dzone.com/posts/show/1799](http://snippets.dzone.com/posts/show/1799)

Fred

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 13, 2007, 3:03am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/5 "2007-11-13T03:03:53Z")

</div>

Frederick C. wrote:

> [@](#):
>
> Have you seen this: [http://snippets.dzone.com/posts/show/1799](http://snippets.dzone.com/posts/show/1799)

That allows you to run view helper methods… specifically I’d like to  
be able to access view helper methods + route helpers edit\_projects and  
be able to use url\_for and link\_to.

I still haven’t worked out a way to do this… i’m wondering if i’ll need  
to reimplement the process function of actioncontroller.base

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [November 13, 2007, 10:20am UTC](https://www.ruby-forum.com/t/running-commands-from-context-of-a-view/119894/6 "2007-11-13T10:20:52Z")

</div>

On 13 Nov 2007, at 02:03, Alex M. wrote:

> Frederick C. wrote:
> 
> > [@](#):
> >
> > Have you seen this: [http://snippets.dzone.com/posts/show/1799](http://snippets.dzone.com/posts/show/1799)
> 
> That allows you to run view helper methods… specifically I’d like to  
> be able to access view helper methods + route helpers edit\_projects  
> and  
> be able to use url\_for and link\_to.

link\_to is a view helper as is one version of url\_for. You just need  
to figure out which modules to include (hint: search through the rails  
code base for the functions you would like to be accessible)

Fred
