F
F
Fahrenhei72016-02-19 21:22:43
Ruby on Rails
Fahrenhei7, 2016-02-19 21:22:43

How to add your method to a Rails application?

Hello. My question is if a user has a :role attribute that has different values. How do I add a method like user.admin? that returns something like self.role == "admin"?
The bottom line is that this method should work in relation to what you call it on. For example.
current_user.admin? (true/false)
@user.admin? (where @user is assigned in the controller) (true/false)
Thank you very much for your help. I appreciate that you took the time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Radushevsky, 2016-02-19
@Fahrenhei7

# models/User.rb
class User
...
  def admin?
    self.role == 'admin'
  end
...
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question