Rails - How to use a Helper Inside a Controller

前端 未结 8 1232
生来不讨喜
生来不讨喜 2020-11-29 15:01

While I realize you are supposed to use a helper inside a view, I need a helper in my controller as I\'m building a JSON object to return.

It goes a little like this:

8条回答
  •  执念已碎
    2020-11-29 15:59

    My problem resolved with Option 1. Probably the simplest way is to include your helper module in your controller:

    class ApplicationController < ActionController::Base
      include ApplicationHelper
    
    ...
    

提交回复
热议问题