如何从HTML加载js.erb

another day, another night spent banging my head over JS and JQuery.

I want to call a page via HTML (and show the route in the header), but have it also load a js.erb script. Or alternatively, I want to call the js.erb script remotely, but also change the http header to indicate the new route.

I want this because: a) I want to learn and this is frustrating me b) currently, when the user refreshes, his current page doesn't come up, but the one from which the remote => true call came in;; it's confusing for the user to go back to a page he doesn't remember.

I'm implementing the JQuery.pageless plugin. I call the user/show page remotely and this is what goes into my show.js.erb file:

$(function(){
$("#container").html("<%= escape_javascript(render '/users/show') %>");
        $('#relationship_list').pageless({ totalPages: "<%= @reviews.count %>"/5+1
                       , url: '/reviews'
                       , params: {id: "<%= params[:id] %>"}
                       , loaderMsg: "loading"
                       });
});

Now this code is A-OK. it works when called remotely from show.js.erb. The problem is that I want to load the "user show" page via html, and not remotely. That means that this script doesn't get loaded. I need to find another, unobtrusive way to load it.

I want to reiterate: there is no bug in the code.

Hopefully someone can help. Also please note: someone wrote about using the gon gem to address the issue. Not for me - I really want to figure this out without gems.

It sounds like you have the page load working via a remote call (I'm assuming an AJAX GET request after clicking a link or button), which works properly, but the URL doesn't update in the address bar?

You're looking for the HTML5 pushState() method.

Update your show.js.erb file like so:

$("#container").html("<%= escape_javascript(render '/users/show') %>");

history.pushState(null, "Page Title", "<%= user_path(@user) %>");