AJAX history state for remote Rails form

I am trying to make browser history (back / forward buttons) for my Ruby on Rails site. I followed the following rails: http://railscasts.com/episodes/246-ajax-history-state

I was able to convert all AJAX links, but I am having problems with remote forms.

Here's what I tried:

edit.html.erb:

<%= simple_form_for(@post,
                    url: post_path(@post.id),
                    remote: true) do |f| %>
  <%= f.input(:comments) %>
  <div class="browser-history-mgmt" 
    <%= f.button(:submit,
                 value: "Save") %>
  </div>
<% end %>

      

application.js:

$(function () {
  $('.browser-history-mgmt input').unbind('click').bind('click', function () {
    var action = this.form.getAttribute("action");
    history.pushState(null, '', action);
    //TODO: call getScript and return false instead of true
    return true;
  });

  $(window).bind("popstate", function () {
    $.getScript(location.href);
  });
})

      

This almost works, but if I try to save without the necessary comments, the address bar is updated to the "show" address while I stay in the "edit" form. (I believe the TODO implementation will fix this ... but I don't understand how to implement this.)

I'm not even sure if I'm on the right track. The railscast episode is now almost 5 years old. Is there an easy way to get the history of browsers working with remote forms in Rails?

+3


source to share





All Articles