ajax output not rendering as it should

I had everything rendering properly before the ajax was introduced. Now the javascript is being rendered out to the html page.

#Here is the list that the items are to be rendered out to   <ul id="task_list"></ul>

#the controller method calling on the rjs file   def show_tasks     @tasks = Task.find(:all, :conditions => ["category_id = ?", params[:id]])   end

#the _task.rhtml file <li>   <%= task.name %>   <%= link_to "View", :controller => "task", :action=> :show, :id => task %>   <%= link_to "Edit", :controller => "task", :action=> :edit, :id => task %>   <%= link_to "Delete", {:controller => "task", :action=> :delete,                         :id=>task},                         {:confirm=>"Are you sure you want to delete this task",                         :method=>:post                         }   %> </li>

#and finally the element that the bad output is rendered to   <div id="task_list">try { Element.update("task_list", "<li>\n blah blah blah\n <a href="%5C%22/ task/show/22%5C%22">View</a>\n <a href="%5C%22/task/edit/22%5C %22">Edit</a>\n <a href="%5C%22/task/delete/22%5C%22" method="\&quot;post\&quot;" onclick='\"return' confirm(="" are="" sure="" you="" want="" to="" delete="" this="" task="" );\="">Delete</

\n</li>\n");

} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"task_list\", \"<li>\n blah blah blah\n <a href="%5C%5C%22/task/show/22%5C%5C%22">View</a>\n <a href="%5C%5C%22/ task/edit/22%5C%5C%22">Edit</a>\n <a href="%5C%5C%22/task/delete/22%5C %5C%22" method="\\&quot;post\\&quot;" onclick='\\"return' confirm(\="" are="" sure="" you="" want="" to="" delete="" this="" task\="" );\ \="">Delete</a>\n</li>\n\");'); throw e }</div>

</div>

Any help is appreciated

Hi Chris,

chris wrote:

Now the javascript is being rendered out to the html page.

#the controller method calling on the rjs file

What does show_tasks.rjs contain? It looks like that may be where your problem is.

Best regards, Bill

Oops forgot about that one, here it is

Hi Chris,

chris wrote:

==== page.replace_html("task_list", :partial => "task", :collection=> @tasks)

Hi Chris,

chris wrote: > Now the javascript is being rendered out to the html page.

> #the controller method calling on the rjs file

What does show_tasks.rjs contain? It looks like that may be where your problem is.

I should have asked you for both views, not just the .rjs. My apologies for the extra turn-around. Your rjs looks fine. What appears to be happening is that rjs is sending the correct stuff to the browser, but the browser is either expecting html rather than js and so is rendering it rather than executing it, or the browser doesn't know how to execute it. For case 1, check the method you're using to invoke the show_tasks method. If it's a link, for example, the method should be link_to_remote, not link_to. Your view needs to be using something that submits using XMLHttpRequest, not a straight POST or GET. That's how the browser knows to expect a js response that it should execute rather than simply display. If that's already in place, then check your application.rhtml or controller-specific layout if applicable. It needs to have this in the <head> section: <%= javascript_include_tag :defaults %>. That includes all the prototype js files you need for the browser to actually execute the js it receives.

Hope that helps, Bill

I do have the javascript_include_tag :defaults

As for the link_to, that is that thing that puzzles me, since the links that appear are hard links to a separate page, it is the hard links appearing that is contained within the ajax. I really don't know why the javascript is rendering out there.

here is the main page, which has a list of categories on the left side, then after clicking on one of them the ajax kicks in and shows the related tasks on the right side

I should also add that the ajax is working fine when clicking on the category links on the left side of the screen (no postback), it is just the task items on the left side of the screen that then render out javascript that is the problem. The html containing the rendered ajax was using an addon in firefox to obtain the html contained within the DOM, but not actually rendered out to the page. (that is kind of a key point, not sure how I left it out before)

Hi Chris,

chris wrote:

I should also add that the ajax is working fine when clicking on the category links on the left side of the screen (no postback),

  <h2>Categories</h2>   <%= link_to "Add Category", :controller => "category", :action => :create %>

Not sure what you mean here. If the above is what you're referring to, there is no Ajax taking place via the category links. Clicking on the "Add Category" link sends a straight Http GET to the server. The server is sending back html which, because it sent a request using Http, the browser is rendering correctly.

it is just the task items on the left side of the screen that then render out javascript that is the problem.

    <%= render :partial => "category", :collection => @categories %>

============= Once again here is the category partial that appears on the left side

<li> <a href="#"   onclick="<%= remote_function :update => 'task_list', :url => {:action => :show_tasks, :id => category} %>"> <%= category.name unless category.nil? %> </a> </li>

In both cases you're using straight links to invoke controller methods which causes the browser to expect an html response. The difference between the situation above and this one is that in the one above you're sending back html and in this one you're sending back js. So...

1) Assuming that you're category object is getting generated by enumerating a instance variable, I think if you replace the <a...></a> with <%= link_to_remote(category.name, :url =>{:action => 'show_tasks', ::id => category}) %> your problem will be fixed. 2) Spend the $9.95 to pick up Cody Fauser's RJS tutorial (in PDF) from O'Reilly. It'll put you on the right track.

Best regards, Bill

I bought the book, and as you mentioned the better way to do it was using the link_to_remote method. All is working now nicely.

Thanks for the help.