我正在尝试使浏览器历史记录(后退/前进按钮)适用于我的 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 年了。有没有一种简单的方法可以让浏览器历史记录在 …
我在我的封闭系统Ruby on Rails应用程序上使用Pundit gem进行授权(使用Rails 4.1.5和Rspec 3.0)
我已经将我的应用程序策略配置为在未按照Pundit文档中的建议定义用户时引发异常:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "must be logged in" unless user
@user = user
@record = record
end
end
Run Code Online (Sandbox Code Playgroud)
如何编写一个规范来验证每个Pundit策略是否拒绝零用户?
我做了以下尝试:
RSpec.describe PostPolicy do
it "should raise an exception when users aren't logged in" do
expect(AccountFolderPolicy.new(nil, record)).to raise_error(Pundit::NotAuthorizedError)
end
end
Run Code Online (Sandbox Code Playgroud)
但它出错了
Failure/Error: expect(PostPolicy.new(nil, record)).to raise_error(Pundit::NotAuthorizedError)
Pundit::NotAuthorizedError:
must be logged in
Run Code Online (Sandbox Code Playgroud)
关于如何正确测试这个的任何建议?
我有以下字符串数组:
x = ["1.2", "1.1", "1.18", "1.18.3", "1.4", "1.18.20", "2.5"]
Run Code Online (Sandbox Code Playgroud)
Ruby的Array排序方法按字母顺序排序字符串,结果如下:
[
[0] "1.1",
[1] "1.18",
[2] "1.18.20",
[3] "1.18.3",
[4] "1.2",
[5] "1.4",
[6] "2.5",
]
Run Code Online (Sandbox Code Playgroud)
我想根据每个小数内的数值对这个数组进行排序,以便它返回以下顺序:
[
[0] "1.1",
[1] "1.2",
[2] "1.4",
[3] "1.18",
[4] "1.18.3",
[5] "1.18.20",
[6] "2.5"
]
Run Code Online (Sandbox Code Playgroud)
我也不能保证每个"ID"中的小数点数.任何帮助将不胜感激.谢谢!