如何实现 coffeescript 在 401 ajaxerror 之后工作?
How to implement coffeescript to work after a 401 ajaxerror?
我正在构建一个 reddit/angellist 克隆。我希望能够在不重新加载页面的情况下进行投票。我通过 votescontroller 进行了 ajax 设置:
respond_to do |format|
format.html { redirect_to request.referer }
format.js
end
然后在 upvote.js.erb:
$(document).ready(function(e) {
$("#vote-sum").html("<%= pluralize(@votable.votes.sum(:value), 'vote') %>");
});
到目前为止一切顺利。单击向上时,总和会立即发生变化。
这一切都发生在用户登录时。当用户未登录时,你按下向上 link,我得到一个 401
,因为我使用 Devise 我有 before_action :authenticate_user!
在我的投票控制器中。我在我的本地服务器上得到这个作为输出:
Started POST "/items/11/upvote" for 127.0.0.1 at 2017-12-02 17:52:06 +0100
Processing by VotesController#upvote as JS
Parameters: {"item_id"=>"11"}
Item Load (1.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = ORDER BY "items"."created_at" DESC LIMIT [["id", 11], ["LIMIT", 1]]
Completed 401 Unauthorized in 24ms (ActiveRecord: 1.2ms)
我想自动将用户引导至登录页面。所以我用谷歌搜索了答案并找到了一个很好的答案:
我必须插入这些代码行:
$ ->
$("a").bind "ajax:error", (event, jqXHR, ajaxSettings, thrownError) ->
if jqXHR.status == 401 # thrownError is 'Unauthorized'
window.location.replace('/users/sign_in') // or with .replace(this)
但是,当我添加这些行时,它不起作用。我将 $("a")
更改为 $(document)
。或者甚至将其更改为香草 JS:
$(function() {
return $(document).bind("ajax:error", function(event, jqXHR, ajaxSettings, thrownError) {
if (jqXHR.status === 401) {
return window.location.replace('/users/sign_in');
}
});
});
旁注:正常的 console.log('!') 和 alert('!') 在 application.js
或 votes.coffee
代码中工作正常。
我希望代码将用户重定向到设计登录页面。
我看过的其他类似资源:
Devise authenticate_user! breaks when making remote: true requests to a non-devise controller action
稍微休息一下,我回来开始研究一些我以前没有看到的其他问题。
解决方法在这里:
在 before_action :authenticate_user!
之前(之前)创建一个方法并实施
def check_user
return if user_signed_in?
redirect_to new_user_session_path, error: 'You must log on to view this part of the site'
end
添加到控制器
class VotesController < ApplicationController
before_action :check_user
...
end
我正在构建一个 reddit/angellist 克隆。我希望能够在不重新加载页面的情况下进行投票。我通过 votescontroller 进行了 ajax 设置:
respond_to do |format|
format.html { redirect_to request.referer }
format.js
end
然后在 upvote.js.erb:
$(document).ready(function(e) {
$("#vote-sum").html("<%= pluralize(@votable.votes.sum(:value), 'vote') %>");
});
到目前为止一切顺利。单击向上时,总和会立即发生变化。
这一切都发生在用户登录时。当用户未登录时,你按下向上 link,我得到一个 401
,因为我使用 Devise 我有 before_action :authenticate_user!
在我的投票控制器中。我在我的本地服务器上得到这个作为输出:
Started POST "/items/11/upvote" for 127.0.0.1 at 2017-12-02 17:52:06 +0100
Processing by VotesController#upvote as JS
Parameters: {"item_id"=>"11"}
Item Load (1.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = ORDER BY "items"."created_at" DESC LIMIT [["id", 11], ["LIMIT", 1]]
Completed 401 Unauthorized in 24ms (ActiveRecord: 1.2ms)
我想自动将用户引导至登录页面。所以我用谷歌搜索了答案并找到了一个很好的答案:
我必须插入这些代码行:
$ ->
$("a").bind "ajax:error", (event, jqXHR, ajaxSettings, thrownError) ->
if jqXHR.status == 401 # thrownError is 'Unauthorized'
window.location.replace('/users/sign_in') // or with .replace(this)
但是,当我添加这些行时,它不起作用。我将 $("a")
更改为 $(document)
。或者甚至将其更改为香草 JS:
$(function() {
return $(document).bind("ajax:error", function(event, jqXHR, ajaxSettings, thrownError) {
if (jqXHR.status === 401) {
return window.location.replace('/users/sign_in');
}
});
});
旁注:正常的 console.log('!') 和 alert('!') 在 application.js
或 votes.coffee
代码中工作正常。
我希望代码将用户重定向到设计登录页面。
我看过的其他类似资源:
Devise authenticate_user! breaks when making remote: true requests to a non-devise controller action
稍微休息一下,我回来开始研究一些我以前没有看到的其他问题。
解决方法在这里:
在 before_action :authenticate_user!
def check_user
return if user_signed_in?
redirect_to new_user_session_path, error: 'You must log on to view this part of the site'
end
添加到控制器
class VotesController < ApplicationController
before_action :check_user
...
end