开发者

sharing private methods across controllers

Hey guys, I have two controllers, X and Y:

In X, I have a :before_filter for a private method, foo.

I also have the following statement at the top of controller X.

helper_method :foo

In controller Y, I have a :before_filter for a private method, bar. In bar, I call foo. But apparently this is not all开发者_Python百科owed as I get an undefined local variable or method error. I would think that my :helper_method declaration should allow foo to be used in other controllers.

Generally, speaking, how can I achieve calling foo in controller Y? Is there a way while still keeping foo private? If not, how is achieved (while adhering to good design principles) if foo is not private?

Thanks!


If you need foo in multiple controllers then you should put it in your base application controller as a protected method. That will give you access to it where you need it while keeping it out of the public interface.

Alternatively, you could put foo in a module and include it as needed.

Which approach you take depends on your specific circumstances.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜