# Using a helper within a controller

**URL:** <https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867>\
**Category:** Rails\
**Created:** [June 27, 2007, 12:01am UTC](https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867 "2007-06-27T00:01:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 27, 2007, 12:01am UTC](https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867/1 "2007-06-27T00:01:20Z")

</div>

I would like to use the link\_to\_remote method in my controller so I can  
update a list within my .rhtml file without having to re-load the entire  
page. Basically, what I am doing is adding stuff to a list and then  
allowing that stuff to be deleted from the list if the user so chooses -  
all with the built-in AJAX functionality. The idea is that I do not want  
the entire page to re-load. I’m thinking that there is an eval() method  
that I can use but have not yet discovered it. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 27, 2007, 12:21am UTC](https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867/2 "2007-06-27T00:21:44Z")

</div>

try something like

render\_to\_string :inline =\> “\<%= link\_to\_remote … %\>”

-fouad

On Jun 27, 1:01 am, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 27, 2007, 6:02am UTC](https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867/3 "2007-06-27T06:02:13Z")

</div>

Shandy N. wrote:

> I would like to use the link\_to\_remote method in my controller so I can  
> update a list within my .rhtml file without having to re-load the entire  
> page. Basically, what I am doing is adding stuff to a list and then  
> allowing that stuff to be deleted from the list if the user so chooses -  
> all with the built-in AJAX functionality. The idea is that I do not want  
> the entire page to re-load. I’m thinking that there is an eval() method  
> that I can use but have not yet discovered it. Anyone have any ideas?

Put the snip of rHTML that needs to refresh into a partial. Include it  
the  
normal way, like this:

 \<%= render :partial =\> 'my\_partial' %\> 

Now wire link\_to\_remote up to an action that looks like this:

def my\_action  
return unless request.xhr?  
render :update do |page| # ← I call that rjs sometimes!  
page.replace\_html ‘refresh\_me’, :partial =\> ‘my\_partial’  
end  
end

The deal is that almost\* anything you can pass to render, you can also  
pass  
to the second argument to JavaScriptGenerator#replace\_html.

The first code injects raw HTML into your page as it renders, before it  
goes  
over the wire. The second snip renders the partial, then creates  
JavaScript  
containing Element.update(‘refresh\_me’, ‘’). This  
goes over the wire, and the Ajax handlers from prototype.js will replace  
the  
innerHTML member of that

 with the new version.

(Question for the lifers - is it _really_ “almost anything”? Or is it  
“anything”!?)

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 27, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/using-a-helper-within-a-controller/107867/4 "2007-06-27T21:33:27Z")

</div>

I tried your idea and it seems to run (or at least it does not complain  
at all about any of the code), but when I attempt to delete the entry  
that I just added, nothing happens.
