Forum: Ruby on Rails Displaying content using AJAX

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4ee5ca38cb149788ca612902d857212e?d=identicon&s=25 vishy (Guest)
on 2009-03-08 16:06
(Received via mailing list)
All,
I am trying to implement some ajax functionality on my web site. I
have a list of links and each link have many items associated with
it.
For example, "My Netflix queue" link is connected to 10 movies
(items). Now, I am trying to display a table with these items when
the
user clicks on the link. I have other links and corresponding items
on
my page. I want to do all this using Ajax. But have not been able to
implement this so far. The code that I have used is given below:

//link.html.erb (:link passes the link_is to the rjs which gets all
//the items for that link)

<% for link in @links %>
<div class="link">
        <div class="link">
                <%= link_to_remote link.name, :url => { :action =>
'show_items', :link => link.id} %>
        </div>
</div>
<% end %>

//this is where I want to display the items

<% if params[:link] %>
<div id="items">
<table>
<% for item in @item %>
        <td>
                <img src="<%=item.image_url%>" /><br />
                <%= item.name %>
        </td>
<% end %>
</table>
</div>
<% end %>

//RJS Template (show_items.js.rjs)
//@items has all the items for that link. I have verified using the
//debug statement. Now I need to pass the @items variable to the view
//so that the items are displayed. But thats where I am erring.

link = params[:link]
@items = Item.find_all_by_link_id(params[:link])
logger.debug "Items : " + @items.inspect
page.replace_html :items, @items, :object => @items

//controller.rb
  def link
    @links = Link.find(:all)
  end
Any help will be greatly appreciated.
Thanks,
vishy
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-08 17:49
(Received via mailing list)
On 8 Mar 2009, at 15:04, vishy wrote:
>
> //this is where I want to display the items
>
> <% if params[:link] %>

This doesn't look right. The placeholder div with id items should
always be there (or else your rjs will have nothing to insert into).
> <% end %>
>
You should be naming a partial here to render.

Fred
This topic is locked and can not be replied to.