# RedCloth + Ajax problem

**URL:** <https://www.ruby-forum.com/t/redcloth-ajax-problem/158027>\
**Category:** Rails\
**Created:** [January 18, 2009, 4:56am UTC](https://www.ruby-forum.com/t/redcloth-ajax-problem/158027 "2009-01-18T04:56:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [January 18, 2009, 4:56am UTC](https://www.ruby-forum.com/t/redcloth-ajax-problem/158027/1 "2009-01-18T04:56:20Z")

</div>

I’m using RedCloth and am trying to update a div (id=‘page\_content’)  
from an ajax link. The problem is that when the element is updated it  
inserts the content twice and contains all kinds of ajax code, newline  
and tab characters with the content. Here is the link:

link\_to\_remote “Exhibitions”, update =\> ‘page\_content’, :url =\>  
{ :controller =\> ‘pages’, :action =\> ‘ajax\_link’, :page\_name =\>  
“exhibitions” }, :loading =\> “Element.show(‘ajax-loader’)”, :loaded =\>  
“Element.hide(‘ajax-loader’)”

the ajax\_link action is:

def ajax\_link  
@page = Page.find\_by\_page\_name(params[:page\_name])  
respond\_to do |format|  
format.js { }  
format.html { render :action =\> ‘show’ }  
end  
end

and the ajax\_link.rjs is:

page.replace\_html :page\_content, :partial =\> ‘page\_content’, :object  
=\> @page

Although if I change the ajax\_link action to the following the div is  
updated correctly:

def ajax\_link  
@page = Page.find\_by\_page\_name(params[:page\_name])  
respond\_to do |format|  
format.js { render :text =\> “#{RedCloth.new  
(@page.page\_content).to\_html}” }  
format.html { render :action =\> ‘show’ }  
end  
end

Thanks for any help,

Stu

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [January 18, 2009, 9:01am UTC](https://www.ruby-forum.com/t/redcloth-ajax-problem/158027/2 "2009-01-18T09:01:17Z")

</div>

If you’re using link\_to\_remote to call an action which has an RJS  
template,  
you need to remove the “update =\> ‘page\_content’,” argument in  
link\_to\_remote. If you specify :update, link\_to\_remote expects to get  
back  
some HTML with which to replace that element of your DOM.

When you leave that off, link\_to\_remote looks for JavaScript (which is  
what  
RJS returns) to then execute on the client-side. That executed  
JavaScript  
then produces the HTML and decides where to put it.

Check out this thread for a (probably) more lucid explanation:

[http://www.ruby-forum.com/topic/95288](http://www.ruby-forum.com/topic/95288)

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [January 19, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/redcloth-ajax-problem/158027/3 "2009-01-19T17:03:52Z")

</div>

Thanks for the help Ryan, works great.
