Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:45:22+00:00 2026-05-28T07:45:22+00:00

My Basic Goal A Rails app that can load new content via Ajax while

  • 0

My Basic Goal

A Rails app that can load new content via Ajax while using HTML5 to maintain the browser’s history state. I have a super basic version of the app running on Heroku. I’ve also set up a public GitHub repository so that all the code mentioned below can be looked at in more detail.

The Problem

I successfully implemented the basic app using the HTML5 browser history API (at one point I also made a version using the History.js jQuery plugin). Everything works as it should until the user clicks a non-ajax link and then hits the back button. For instance, if the user clicked item 2, the app successfully loads the new content and changes the browser state. If they hit the back button, it also correctly reloads the previous content and changes the browser state back to match. But if the user then browses to another webpage (any other page on the internet) then clicks their browser’s back button, the Rails app renders raw javascript instead of the original page.

Everything works up until that last step, when the controller starts returning the raw javascript from show.js.erb instead of show.html.erb. I’ve tried changing several settings in the controller, but I can’t figure out what specifically is causing this. I do know that if I remove the history.pushState call in show.js.erb, the problem stops (though obviously I lose all the browser history functionality I was looking for in the first place).

Any ideas?

Bonus: Excruciating Detail

This is how it all works (I think!).

The user clicks on a link, which looks like this under the hood:

<%= link_to number_to_human(volume.number), volume_path(volume), :remote => true %>

This sends a javascript request to the controller, which looks like this:

def show
  @volume = Volume.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.js # show.js.erb
    format.json { render json: @volume }
  end
end

This passes the request to show.js.erb which looks like this:

var stateObject = {"html": "<%= escape_javascript render(@volume) %>"};
history.pushState(stateObject,'',"<%= escape_javascript volume_path(@volume) %>");
$('#volumes').fadeTo(0, 0);
$('#volumes').html(stateObject.html).fadeTo('slow', 1.0);

Which then replaces the content of a div tag on the original page and uses pushState to create a state object and change the page URL.

The user can then click the back button and javascript attached to the window.onpopstate event in show.html.erb will reload the appropriate content:

    if(history.pushState) {
    window.onpopstate = function(event) {  
        if (event.state != null) {
            $('#volumes').html(event.state.html);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:45:23+00:00Added an answer on May 28, 2026 at 7:45 am

    I’m very new to RoR so I’m not sure if my answer would be the best, but I ran into the same problem and managed to fix it. So here’s my two cents.

    It seems showing raw ajax response happens because of browser cache. I fixed the problem by adding the following header to prevent the response from begin cached.

    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Provide a web service using Visual Basic or C# or .NET that interacts
I have a basic form that I am using as a dummy to test
Background: Here the goal is to do some basic commands in powershell using select-string.
I worked through some basic tutorials on Rails 3. The goal is a community-website
Basic Goal is that i have four progress bar and want to run them
The goal: I'm trying to use the new Entity Framework 4.1 DbContext API (using
My goal is to build a standalone RESTful Rails 3 service that communicates with
I am parsing a text (css) file using fscanf. The basic goal is simple;
We have a basic data entry app we want to develop using QT as
Basic question: How do I load an Excel template for use with POI and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.