Topic: updating a div on a page with an item description using ajax

So I am new to rails but I figure the best way to learn is to just make something!

I am working on a todo list and so far I have the ability to create a new item, drag them into the order you want them in using ajax and have that update their position in the database.

Now I want to add a link to each item that will display details about the item. I am storing this in another table called details and retrieving the information when needed. I am using a foreign key to link the details table to the id in the todos table.

Currenly when you click the 'View Details' link it attemps to download and save a file (no clue why) but the file contains, among other things, the detail from the table.

What am I missing here?

*** _display partial

<% @item = display %>

<li id="item_<%= @item.id %>">
    <%= check_box("item", "done", "onclick" => "document.location.href='/todo/toggle_check/#{@item.id}'") %>
    <%= @item.description %>
    <%= link_to("View Details", :action => "view_details", :id => @item.id) %>
    <%= link_to("Edit", :action => "edit", :id => @item.id) %>
    <%= link_to("Delete", { :action => "delete", :id => @item.id },
               :confirm => "Are you sure you want to delete this entry: #{@item.description}") %>
</li>


*** Controller todo

def view_details
  @details = Detail.find_item_description(params[:id])
end


*** Model detail

def self.find_item_description(id)
  detail = find(:first, :conditions => [ "todo_id = ?", id])
end


*** view_details.rjs

page.replace_html("details", :partial => "details", :object => @details)


*** _details partial

<%= @details.item_detail %>



There is an empty div with an id="details" in the list view

Last edited by rburbach (2007-01-07 21:06:52)

Re: updating a div on a page with an item description using ajax

You should be using link_to_remote instead of link_to if you are making an ajax call.

ProgrammingBooks.org - Programming Books Ranked by Programmers
Turn of the Crank - My Personal Blog
Robot Walrus - My art prints & posters blog
My Game Ideas - Submit, rate, view video game Ideas

Re: updating a div on a page with an item description using ajax

nice catch, I missed that...

my ajax call should look like:

link_to_remote("View Details", :update => "details", :url => {:action => "view_details", :id => @item.id})


This works, but I am getting wierd output from the database, this is being placed in the "details" div:

try { Element.update("details", "Create a details div and and rjs file to populate it with details for the todo item\n"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"details\", \"Create a details div and and rjs file to populate it with details for the todo item\n\");'); throw e }

Anyone know why this is?

Last edited by rburbach (2007-01-07 21:05:00)

Re: updating a div on a page with an item description using ajax

If you're using RJS, don't specify the :update parameter in link_to_remote:

link_to_remote("View Details", :url => {:action => "view_details", :id => @item.id})

Railscasts - Free Ruby on Rails Screencasts

Re: updating a div on a page with an item description using ajax

Thanks again Ryan - i figured it out after much reading and frustration late last night, just hadn't got a chance to post it yet! Thanks again though!

Re: updating a div on a page with an item description using ajax

I have a

<% form_remote_tag :url => { :action => :create }   do %>

and my create.rjs does a

page.replace_html 'flash-notice', @message

and instead of updating the div 'flash-notice' with the message

I get

try {
Element.update("flash-notice", "Invalid login or password from rjs");
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"flash-notice\", \"Invalid login or password from rjs\");'); throw e }

Can anyone explain what is happening?

Thanks