Best Practices for reusing code between controllers in Ruby on Rails

前端 未结 7 721
忘掉有多难
忘掉有多难 2020-11-29 16:06

I have some controller methods I\'d like to share. What is the best practice for doing this in ruby on rails? Should I create an abstract class that my controllers extend, o

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 16:37

    I actually think a module is the best way to share code amongst controllers. Helpers are good if you want to share code amongst views. Helpers are basically glorified modules, so if you don't need view level access, I suggest placing a module in your lib folder.

    Once you create the module, you'll have to use the include statement to include it in the desired controllers.

    http://www.rubyist.net/~slagell/ruby/modules.html

提交回复
热议问题