0

RoRサイトにデバイスベースの認証システムがあり、しばらく操作がない場合はユーザーを自動サインアウトする必要があります。しかし、私のサイトには長い間開かれるように作成されたページがいくつかあり(ユーザーはページを見るだけで、情報はajaxによって更新されます)、このページが開かれたときにユーザーをサインアウトしたくありません。

誰かがそれを行う方法を知っていますか?または、ajaxリクエストもユーザーアクティビティであることをDeviseに伝える方法はありますか?

4

1 に答える 1

1

まず、1.5.2がインストールされていることを確認します。インストールされていない場合は、アップグレードします。この質問では6か月です:-)そしてすでに問題が解決されていることを願っています。

自動サインアウトを防止したいページにいるときに、timeout_inの戻り値を変更できます。

例えば:

class Student < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable

  def timeout_in
      if session[:auto_sign_out] then
         #the normal period of signout
         30.minutes
      else
         #very long period, to prevent sign-out
         100.years
      end
  end
end

次に、ApplicationControllerにbefore_filterを簡単に追加して、session[:auto_sign_out]値を設定できます。

このような:

before_filter :manage_page_auto_sign_out


def manage_page_auto_sign_out
# check the params, and see if the id is the id of the specific page
if params[:id] == EXCLUDED_PAGE_ID then
session[:auto_sign_out]= false
else
session[:auto_sign_out]= true
end
end

さらに、次のようにページのコントローラー名をチェックすることで、目的のページに対してチェックしていることを確認するために他の条件を追加できます。

def manage_page_auto_sign_out
# check the params, and see if the id is the id of the specific page
if controller_name == 'pages' && params[:id] == EXCLUDED_PAGE_ID then
session[:auto_sign_out]= false
else
session[:auto_sign_out]= true
end
end

ページコントローラーの名前を確認する必要があります。これがお役に立てば幸いです。

于 2011-12-02T04:53:30.563 に答える