简体   繁体   中英

devise and current_user

I have a Rails 3 app that I am trying to implement devise and declarative_authorization. An important part of declarative_authorization is the presence of a function "role_symbols" within the user model. Because of the way I implement roles, I am implementing an instance method within the User model to keep track of a value (let's call foo) as such:

  attr_accessor :foo

  def foo=(val)
    @foo = val
  end

  def foo
    @foo
  end

Then we will use the value of foo inside the role_symbols method to limit the valid roles, maybe like this:

def role_symbols
  roles.where("foo = ?", @foo).name.underscore.to_sym
end

The issue is when I try to set the value of foo for the current_user in a controller, the value doesn't stick, for example:

current_user.foo = 99

is successful, but when I check the value in another view (or controller), the value of current_user.foo is nil.

Isn't the current_user object just a User object that is persisted in the Session? If so, is there some lock on setting instance values within the current_user object?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM