Ruby on rails 轨道3和x2B;设计:在登录和注销时更新数据库属性

Ruby on rails 轨道3和x2B;设计:在登录和注销时更新数据库属性,ruby-on-rails,Ruby On Rails,每当用户登录或注销时,我想将“用户”表中的“状态”属性更改为“联机”或“脱机”。 我通过向相关方法添加update_属性创建了自己的会话控制器: class SessionsController < Devise::SessionsController def sign_out_and_redirect(resource_or_scope) current_user.update_attribute(:status, "Offline" ) scope = Devise

每当用户登录或注销时,我想将“用户”表中的“状态”属性更改为“联机”或“脱机”。 我通过向相关方法添加update_属性创建了自己的会话控制器:

class SessionsController < Devise::SessionsController
  def sign_out_and_redirect(resource_or_scope)
    current_user.update_attribute(:status, "Offline" )
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
    redirect_for_sign_out(scope)
  end

  def redirect_for_sign_out(scope)
    redirect_to after_sign_out_path_for(scope)    
  end

  def sign_in_and_redirect(resource_or_scope, *args)
    options  = args.extract_options!
    scope    = Devise::Mapping.find_scope!(resource_or_scope)
    resource = args.last || resource_or_scope

    if warden.user(scope) == resource
      expire_session_data_after_sign_in!
    else
      sign_in(scope, resource, options)
    end

    current_user.update_attribute(:status, "Online" )

    redirect_for_sign_in(scope, resource)
  end

  def redirect_for_sign_in(scope, resource)
    redirect_to stored_location_for(scope) || after_sign_in_path_for(resource)
  end

end
我该如何解决这个问题

当我删除第一个未定义的方法时,我只有:

sign_out(scope)
然后就可以注销了。但我不想删除用于登录的表达式


我该怎么办?有没有其他方法可以跟踪用户的在线状态?

刚刚解决了我的问题,只需将sessions\u controller.rb中的所有内容放到helper/sessions\u helper.rb中,然后添加方法sign\u out\u all\u scopes,并在登录后过期\u session\u data\u!从Desive API到helper。

好吧,我以为我解决了我的问题,但它只工作了几次。。。一段时间后,它停止工作,并且在创建新用户后,status属性为nil,或者在用户登录或注销时不想更改
sign_out(scope)