# AJAX and REST: rendering partials

**URL:** <https://www.ruby-forum.com/t/ajax-and-rest-rendering-partials/152038>\
**Category:** Rails\
**Created:** [October 29, 2008, 11:51am UTC](https://www.ruby-forum.com/t/ajax-and-rest-rendering-partials/152038 "2008-10-29T11:51:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 29, 2008, 11:51am UTC](https://www.ruby-forum.com/t/ajax-and-rest-rendering-partials/152038/1 "2008-10-29T11:51:59Z")

</div>

Hi,  
I have an Items resource and each Item has an attribute ‘starred’. The  
items/index page shows a list of items, each one is in its own partial  
and has an image of a star (star\_off.gif) next to it. When the star is  
clicked, an AJAX PUT request is made to the Update action. The starred  
attribute is updated and a the items partial is replaced so that  
star\_on.gif is shown. So far so good, the code is below:

#### \_item.html.erb
 \<% if item.starred?%\> \<%= link\_to\_remote image\_tag('star\_on.gif',:class =\> 'star'), :url =\> item\_url(item), :with =\> "'item[starred]=false'", :method =\> :put

```
 %>
<% else %>
  <%= link_to_remote image_tag('star_off.gif',:class => 'star'),
                     :url => item_url(item),
                     :with => "'item[starred]=true'",
                     :method => :put
   %>
<%end%>
<%= link_to item.name, item_path(item) %>

```

#### items\_controller

def update  
@item = Item.find(params[:id])  
respond\_to do |format|  
if @item.update\_attributes(params[:item])  
format.js {render :update do |page|  
page.replace “item\_#{@item.id}”, :partial =\>  
‘shared/item’, :locals =\> {:item =\> @item}  
end}  
else  
end  
end  
end

The problem comes when I am working on the show.html.erb page. This  
page shows the details of the item. I want show the star on this page  
too and have the same functionality, i.e. the stars state changes when  
the image is clicked. However, the controller is coded to render the  
shared/item partial when a javascript request is received, this is not  
what I want on the show page. The \_item.html.erb is for the index page,  
I do not want to render this when the request comes from the show page.

Am I approaching the AJAX calls in the correct way here? The partial  
that needs to be rendered after the update action is dependent on where  
the AJAX call is made from. How is this done, or am I doing something  
completely wrong?  
Thanks

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 29, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/ajax-and-rest-rendering-partials/152038/2 "2008-10-29T17:24:27Z")

</div>

Hi Tim,

Tim C. wrote:

> The partial that needs to be rendered after the update action  
> is dependent on where the AJAX call is made from.

The easy way to handle this is simply to pass a param, probably via a  
hidden\_field\_tag, that tells you from which partial the method was  
called.  
There’s nothing unRESTful about this. The only downside is that it’ll  
push  
you to do your rendering in the controller rather than just letting  
Rails  
render the default view. Nothing really wrong with that either though.  
The  
method will look something like…

def update  
respond\_to do |format|  
format.js {  
if params[:called\_from] == ‘show’  
render\_update do |page|  
page.replace\_html …  
end  
elsif params[:called\_from] == ‘list’  
render\_update do |page|  
page.replace\_html … something else  
end  
else  
#might want to do some error trapping here  
end  
}  
end  
end

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 29, 2008, 7:43pm UTC](https://www.ruby-forum.com/t/ajax-and-rest-rendering-partials/152038/3 "2008-10-29T19:43:12Z")

</div>

Thanks for the help, I have implemented it in the way you suggested.
