5

Question, can you have private and protected in a single Ruby on Rails controller? If not, which one is preferred in a devise controller, or a regular controller for a model?

Thanks

Corey
  • 835
  • 1
  • 9
  • 32
  • A Rails controller is just a Ruby class. You can have anything in a Rails controller that you can have in a Ruby class, so yes, you can have both in the same class, and the reasons to use one or the other are the same as they are for any Ruby class. – Jordan Running Feb 10 '17 at 16:36

1 Answers1

16

can you have private and protected in a single Ruby on Rails controller?

Yes, you can. Rails controllers are just classes, and classes can have any number and combination of private and protected blocks.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • This is an incorrect answer. Inherited controller will own a method, even if it is private in parent class. Protected methods can be called by OTHER object of this or inherited class. You don't usually instantiate controllers and communicate between them on your own. Thus, contorollers usually do not need protected methods at all. Use `private` there. – Ngoral Sep 27 '22 at 12:34
  • @Ngoral You're right of course, thanks for pointing that out. – user229044 Sep 27 '22 at 13:31