views:

50

answers:

2

I'm trying to fire an AJAX request when my page loads. I'm guessing this would be best from my controller.

First I open a new page..

def text_tabs_nav
end

This page has the ability to load by AJAX two different partials. I want to load one of them on default.

So I thought maybe this ..

def text_tabs_nav
  respond_to do |format|
    format.html
    format.js
  end
end

And in my text_tabs_nav.js I wrote this :

== $("#content").html("#{ escape_javascript(render :partial => 'about_us') }");

But the javascript didn't load..I use that same javascript to load things after a page has been loaded, and it works fine.

Anyone have any idea how I could load a partial ( text_tabs_nav ), with a default AJAX partial loaded within ( about_us ) ?

Thanks!

A: 

It isn't being parsed because it's in a vanilla js file. What you want is

text_tabs_nav.js.erb

Jamie Wong
Vanilla? Hmm.. my file is called text_tabs_nav.js though. I switched my controller to format.js.erb and the file to that as well and it didn't render. Returned undefined method `erb' for #<Proc:0x2e8914c>
Trip
I'm not following... Your file SHOULD be called `text_tabs_nav.js.erb`. Otherwise rails assumes it's just a regular javascript file with no ruby in it to process. The line `format.js` stays the same. Whenever you render anything, it will look for that file (e.g. "text_tabs_nav.js") with no extensions, or with any templating extension (.erb, .haml).
Jamie Wong
Ah.. I forgot to mention I'm running on haml/sass. Which is a step closer, I renamed it to `text_tabs_nav.js.haml` . So if I remove the format.html from my controller, its loading in the right content. But with it, it doesn't actually load the AJAX into the page.
Trip
I strongly recommend you use `erb` for the javascript. Given that you are using haml though, I don't know why you have `==` at the beginning of the line.
Jamie Wong
I appreciate the recommendations. I'm assuming that HAML JS doesn't need the "==". I think the problem is probably because the ajax required to load the page has to happen after the page is loaded. And since I want it on default, I don't want to put it in the view itself since other partials have to be loaded there as well. I appreciate your comments nonetheless
Trip
If your file is called `text_tabs_nav.js.haml` then you just need to put a `:javascript` filter before the code. The Ruby code will be run inside the `#{}` expansions. See http://railsnotes.com/474-javascript-haml/ for an example
bjg
A: 

Well in a glorious blaze of defeat, I ditched the controller, an added a regular call in my view.

:javascript
  $("#content").html("#{ escape_javascript(render :partial => 'about_us') }");

Problem solved.

Trip