Twi*_*ceB 5 javascript ajax jquery ruby-on-rails browser-history
我正在尝试使浏览器历史记录(后退/前进按钮)适用于我的 Ruby on Rails 站点。我遵循以下 rails cast 剧集:http : //railscasts.com/episodes/246-ajax-history-state
我能够转换所有 AJAX 链接,但是我遇到了远程表单的问题。
这是我的尝试:
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 %>
Run Code Online (Sandbox Code Playgroud)
应用程序.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);
});
})
Run Code Online (Sandbox Code Playgroud)
这几乎有效,但如果我尝试在没有所需注释的情况下保存,地址栏会更新为“显示”地址,而我仍停留在“编辑”表单上。(我相信实施 TODO 会解决这个问题……但我不知道如何实施。)
我什至不确定我是否在这里走正确的道路。铁路广播剧集现在已经快 5 年了。有没有一种简单的方法可以让浏览器历史记录在 Rails 中处理远程表单?