# New AJAX function: remote\_replace\_with\_partial() \<-- write l

**URL:** <https://www.ruby-forum.com/t/new-ajax-function-remote-replace-with-partial-write-l/56952>\
**Category:** Rails\
**Created:** [April 6, 2006, 1:08am UTC](https://www.ruby-forum.com/t/new-ajax-function-remote-replace-with-partial-write-l/56952 "2006-04-06T01:08:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Larson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f6c823/32.png) [@Chris\_Larson](https://www.ruby-forum.com/u/Chris_Larson)\
**Post date:** [April 6, 2006, 1:08am UTC](https://www.ruby-forum.com/t/new-ajax-function-remote-replace-with-partial-write-l/56952/1 "2006-04-06T01:08:55Z")

</div>

I hate writing controller actions that support simple lightweight  
ajax calls like this:

def show\_buttons  
render :partial =\> “home\_buttons”  
end

So the birth of remote\_replace\_with\_partial  
Now you can replace a div elements with any controllers partials and  
pass those partials any number of params without having to write  
supportive actions in your controllers. This is a great tool for UI  
changes on the fly and cancel buttons

Heres the code:  
application\_helper file:  
def remote\_replace\_with\_partial(link, options = {}, params = {})  
## For passing params ##  
param\_string = String.new  
params.each {|key,value| param\_string \<\< “&#{key}=#{value}” }

```
 string = link_to_function(link, "update_page_element('#{options

```

[:update]}’, ‘#{options[:controller]}’, ‘#{options[:partial]}’, ‘#  
{param\_string}’);")  
string  
end

application.rb file:  
def partial\_handler  
string = render\_to\_string :partial =\> params[:partial]  
render :inline =\> string  
end

javascript code: (this can be placed in a JS file or in your template)  
\<%= javascript\_tag "function update\_page\_element(element\_id,  
controller ,partial\_file, params)  
{  
new Ajax.Updater(element\_id, ‘/’ + controller + ‘/partial\_handler?  
partial=’ + partial\_file + params, {asynchronous:true,  
evalScripts:true}); return false;  
} " %\>

View code:  
\<%= remote\_replace\_with\_partial(‘change me ajax’, {:update =\>  
‘tag1’, :controller =\> ‘forms’, :partial =\> ‘test’}, { :name =\> ‘joe  
black’, :id =\> 2}) %\>

Please provide as much feedback as possible – I’m a noobe rails hacker

Enjoy,  
Chris Larson

---

<div class="post-metadata">

**Author:** ![Chris\_Larson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f6c823/32.png) [@Chris\_Larson](https://www.ruby-forum.com/u/Chris_Larson)\
**Post date:** [April 6, 2006, 8:26am UTC](https://www.ruby-forum.com/t/new-ajax-function-remote-replace-with-partial-write-l/56952/2 "2006-04-06T08:26:42Z")

</div>

Looks good - a high-level comment is that it might be a deviation from  
MVC tho?
