Displaying content using rails


#1

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 %>

<%= link_to_remote link.name, :url => { :action => 'show_items', :link => link.id} %>
<% end %>

//this is where I want to display the items
<% if params[:link] %>

<% for item in @item %> <% end %>

<%= item.name %>
<% 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


#2

Hi Vishy,

On Sat, 2009-03-07 at 22:26 -0800, vishy wrote:

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:

I recommend you pick up Cody F.'s ‘RJS Templates for Rails’ short
cut at O’Reilly (http://oreilly.com/catalog/9780596528096/) $10 will
get you a PDF download that will get you well down the road on Ajax for
Rails.

HTH,
Bill