# Ajax problem - works once, but not again

**URL:** <https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368>\
**Category:** Rails\
**Created:** [April 7, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368 "2007-04-07T16:32:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zavulon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/977dab/32.png) [@zavulon](https://www.ruby-forum.com/u/zavulon)\
**Post date:** [April 7, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368/1 "2007-04-07T16:32:34Z")

</div>

Hi all,

I have a problem with render partial and all that good stuff. I have a  
link that updates a number (increment rating) when clicked in an  
Ajaxy-way, without need to refresh. Clicking it for the first time works  
perfectly, but after it updates the number, if I click it again (it’s  
supposed to remove rating) - it doesn’t work. Only if I refresh the  
screen, it works properly (removes the rating).

Here’s the code (I tried to remove as much irrelevant stuff as  
possible):

\_items\_ratings.rhtml:

```
... rating here ...

.. try to find an existing rating ...

<% if @rating && @rating.value == 1 %>
    <%= link_to_remote (image_tag("/images/arrow-up-d.gif"), :update

```

=\> this div, :url =\>{ :controller =\> :items, :action =\> :remove\_rating,  
:id =\> item.id})%\>  
\<% else %\>  
\<%= link\_to\_remote (image\_tag("/images/arrow-up.gif"), :update  
=\> this div, :url =\>{ :controller =\> :items, :action =\>  
:increment\_rating, :id =\> item.id})%\>  
\<% end %\>

items\_controller:

def increment\_rating  
@item = Item.find(params[:id])  
redirect\_to :controller =\> ‘rating’, :action =\> ‘increase’, :item\_id  
=\> @item.id  
end

def remove\_rating  
@item = Item.find(params[:id])  
redirect\_to :controller =\> ‘rating’, :action =\> ‘remove’, :item\_id  
=\> @item.id  
end

rating\_controller:

def increase

```
.. increase rating in the db ...

render(:partial => 'items/items_ratings', :locals => { :item =>

```

@item, :id =\> @item.id })  
end

def remove

```
.. remove rating from the db ...

render(:partial => 'items/items_ratings', :locals => { :item =>

```

@item, :id =\> @item.id })

end

---

<div class="post-metadata">

**Author:** ![zavulon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/977dab/32.png) [@zavulon](https://www.ruby-forum.com/u/zavulon)\
**Post date:** [April 7, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368/2 "2007-04-07T19:51:16Z")

</div>

Not sure if this is the cause of the problem but if the div "

 " is inside the partial items\_ratings and it is the target of  
link\_to\_remote update when you click the link the page is going to  
look like 

 ... rating here ...

```
.. try to find an existing rating ...

```

You may have taken out too much information, whats the div  
link\_to\_remote is updating?

- Richard

---

<div class="post-metadata">

**Author:** ![zavulon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/977dab/32.png) [@zavulon](https://www.ruby-forum.com/u/zavulon)\
**Post date:** [April 7, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368/3 "2007-04-07T20:05:20Z")

</div>

Richard L. wrote:

> Not sure if this is the cause of the problem but if the div "
> 
> " is inside the partial items\_ratings and it is the target of  
> link\_to\_remote update when you click the link the page is going to  
> look like 
> 
> ... rating here ...
> 
> ```
> .. try to find an existing rating ...
> 
> ```
> 
> You may have taken out too much information, whats the div  
> link\_to\_remote is updating?
> 
> - Richard
> 
> Ah, that looks to be the case! I see that the div is indeed repeated  
> twice. Here’s a code that names the div (on the main page there’s a  
> listing of items, that’s why I have to dynamically construct the div  
> name):
> 
> \_item.rhtml that contains \_items\_ratings.rhtml:
> 
> \<% @rating\_box\_id = ‘rating\_box\_’ + item.id.to\_s %\>
> 
> …
> 
> \<%= render(:partial =\> ‘items\_ratings’, :locals =\> { :item =\> item,  
> :id =\> item.id }) -%\>
> 
> Inside \_items\_ratings.rhtml:
> 
> "\>
> 
> ```
> ...
> 
> <%= link_to_remote (image_tag("/images/arrow-up-d.gif"), :update=> 
> 
> ```
> 
> @rating\_box\_id, :url =\>{ :controller =\> :items, :action =\>  
> :remove\_rating, :id =\> item.id})%\>
> 
> ```
> ...
> 
> ```
> 
> I think I understand what the problem is… but how do I fix it?

---

<div class="post-metadata">

**Author:** ![zavulon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/977dab/32.png) [@zavulon](https://www.ruby-forum.com/u/zavulon)\
**Post date:** [April 8, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/ajax-problem-works-once-but-not-again/91368/4 "2007-04-08T16:26:43Z")

</div>

Any ideas, anyone?
