# How to ajaxify?

**URL:** <https://www.ruby-forum.com/t/how-to-ajaxify/140552>\
**Category:** Rails\
**Created:** [June 16, 2008, 5:57am UTC](https://www.ruby-forum.com/t/how-to-ajaxify/140552 "2008-06-16T05:57:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [June 16, 2008, 5:57am UTC](https://www.ruby-forum.com/t/how-to-ajaxify/140552/1 "2008-06-16T05:57:32Z")

</div>

Hi all.

I’m doing my own blog-like site, so I’ve an Post controller and an  
Comments  
controller and I want to ajaxify my comments form. So, I’ve did the  
following:

[comments\_controller.rb]  
class CommentsController \< ResourceController::Base  
belongs\_to :post, :article, :photo

create do  
flash “Comment posted successfuly”  
wants.html { redirect\_to :back }  
wants.js { render :layout =\> false }  
end  
end  
[/comments\_controller.rb]

[create.js.rjs]  
page.replace\_html(“comments”, :partial =\> “comment”, :collection =\>  
@post.comments)  
[/create.js.rjs]

[views/posts/show.html.erb]  
\<%= render :partial =\> “post”, :object =\> @post %\>

### Comments

 \<%= render :partial =\> "comments/comment", :collection =\> @post.comments %\>

\<% remote\_form\_for([@post,Comment.new], :update =\> “new\_comment”) do |f|  
%\>  
\<%= render :partial =\> “comments/form”, :locals =\> { :f =\> f } %\>

\<%= submit\_tag "Create" %\>

\<% end %\> [/views/posts/show.html.erb]

Please, also note that I’m using resource\_controller plugin.

When I write down my comment, comments\_partial is updated and my new  
comment  
appears, but I also got the following:

[output]  
Element.update(“comments”, “\u003Ca  
href="[http://presto.stellar.com.br](http://presto.stellar.com.br)"\u003Edavi\u003C/a\u003E said, in  
Post  
Fouth post at\nSun Jun 15 12:04:33 -0300  
2008:\u003Cbr/\u003E\nCommenting!  
I’m enjoying Rails!!! Finaly!! =D\n\u003Cbr/\u003E\u003Cbr/\u003E\u003Ca  
href="[http://presto.stellar.com.br](http://presto.stellar.com.br)"\u003Edavi\u003C/a\u003E said, in  
Post  
Fouth post at\nSun Jun 15 12:05:25 -0300 2008:\u003Cbr/\u003E\nI’ve  
learned  
how to restart Passenger… =)\n\u003Cbr/\u003E\u003Cbr/\u003E\u003Ca  
href="[http://my.coolsite.com](http://my.coolsite.com)"\u003Eauthor\u003C/a\u003E said, in Post  
Fouth  
post at\nMon Jun 16 00:50:43 -0300 2008:\u003Cbr/\u003E\nmy  
comment\n\u003Cbr/\u003E\u003Cbr/\u003E”);  
[/output]

and my comments’ form desapair.

What am I missing here?

Please, sorry for my very bad English.

## Best regards,

## Davi V.

E-mail: removed\_email\_address@domain.invalid  
MSN : removed\_email\_address@domain.invalid  
GTalk : removed\_email\_address@domain.invalid  
Skype : davi vidal  
YIM : davi\_vidal  
ICQ : 138815296

---

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [June 16, 2008, 9:47am UTC](https://www.ruby-forum.com/t/how-to-ajaxify/140552/2 "2008-06-16T09:47:40Z")

</div>

On Jun 16, 4:56 am, Davi V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> comment\n\u003Cbr/\u003E\u003Cbr/\u003E");  
> [/output]

Is that what is inserted into your comments div? The thing here is  
that ajax in rails can be used in one of two ways. The first is that  
you give remote\_form\_for, link\_to\_remote etc… a dom id via  
the :update option and then response from your action replaces the  
contents of that page element. In this case you action should just  
render an html fragment. The second way is your action renders  
javascript, and the javascript on the page executes it when it comes  
back (rjs is one way of generating javascript. Here you’re mixing the  
two: you’re specifying :update but then you’re rendering rjs. Pick one  
or the other.

Fred

---

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [June 16, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/how-to-ajaxify/140552/3 "2008-06-16T18:16:14Z")

</div>

Em Monday 16 June 2008, Frederick C. escreveu:

> > =)\n\u003Cbr/\u003E\u003Cbr/\u003E\u003Ca  
> > href="[http://my.coolsite.com](http://my.coolsite.com)"\u003Eauthor\u003C/a\u003E said, in Post  
> > Fouth post at\nMon Jun 16 00:50:43 -0300 2008:\u003Cbr/\u003E\nmy  
> > comment\n\u003Cbr/\u003E\u003Cbr/\u003E");  
> > [/output]
> 
> Is that what is inserted into your comments div?

Yes.

> The thing here is that ajax in rails can be used in one of two ways.  
> The first is that you give remote\_form\_for, link\_to\_remote etc…  
> a dom id via the :update option and then response from your action  
> replaces the contents of that page element. In this case you action  
> should just render an html fragment. The second way is your action  
> renders javascript, and the javascript on the page executes it when  
> it comes back (rjs is one way of generating javascript. Here you’re  
> mixing the two: you’re specifying :update but then you’re rendering  
> rjs. Pick one or the other.

I’ve droped down :update and all is working fine now.

Thank you very much, Fred.

## – Davi V.

E-mail: removed\_email\_address@domain.invalid  
MSN : removed\_email\_address@domain.invalid  
GTalk : removed\_email\_address@domain.invalid  
Skype : davi vidal  
YIM : davi\_vidal  
ICQ : 138815296
