# Ajax replacing partial not working

**URL:** <https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140>\
**Category:** Rails\
**Created:** [August 15, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140 "2007-08-15T21:28:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 15, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140/1 "2007-08-15T21:28:07Z")

</div>

I am trying to get a comment system going for a page and I want to use  
ajax to replace the partial holding the comments. When I press submit my  
little indicator icon pops up and nothing happens, but if I refresh the  
page the comment is there.  
This is the code I have for the comment in my controller:  
def comment  
id = params[:id]  
user\_id = params[:user\_id]  
comment = params[:comment]  
arr = {:place\_id =\> id, :user\_id =\> user\_id, :comment =\> comment,  
:time\_added =\> Time.now}  
place\_comment = PlaceComment.new(arr)  
place\_comment.save  
bar = Bar.find\_by\_id(id)  
render :update do |page|  
page.replace\_html ‘comments’, :partial =\> ‘\_comments’, :object =\>  
bar  
end  
end

My partial called comments.rhtml looks like this:

 \<% if !@bar.place\_comments.nil? %\> \<% for comments in @bar.place\_comments %\> 

| \<%= image\_tag url\_for\_file\_column(comments.user, "photo", "thumb") unless comments.user.photo.nil? %\> | \<%= h comments.user.username %\> said: |

| \<%= h comments.comment %\> |

  
 \<% end %\> \<% end %\>

And I have a div wrapping the partial where the id is called “comments”,  
I can’t see what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 15, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140/2 "2007-08-15T21:45:33Z")

</div>

You don’t need the underscore when referencing a partial this way.

Change: page.replace\_html ‘comments’, :partial =\>  
‘\_comments’, :object =\> bar

To this: page.replace\_html ‘comments’, :partial =\>  
‘comments’, :object =\> bar

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 15, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140/3 "2007-08-15T22:19:19Z")

</div>

Aaron W. wrote:

> You don’t need the underscore when referencing a partial this way.
> 
> Change: page.replace\_html ‘comments’, :partial =\>  
> ‘\_comments’, :object =\> bar
> 
> To this: page.replace\_html ‘comments’, :partial =\>  
> ‘comments’, :object =\> bar

That didn’t work, and I thought you did have to use the underscore using  
rjs rendering (according to what I read in AWD) correct me if I’m wrong.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 15, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140/4 "2007-08-15T23:00:51Z")

</div>

I had this problem for awhile and it was because of the underscore.  
You might want to specify the controller when calling that partial and  
see if that works.

:partial =\> ‘controllerName/comments’

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 15, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/ajax-replacing-partial-not-working/112140/5 "2007-08-15T23:13:17Z")

</div>

Ok I finally got it, the problem was that I was passing in a bar object  
to the partial in the ajax updater controller method and the partial had  
no clue what to do with it, so I simply had to pass it an @bar instead  
of bar object and viola working fine now. Thanks for the help.
