![](/img/trans.png)
[英]How to call methods in ApplicationController from other controllers?
[英]How to call ApplicationController methods from ApplicationHelper
我想在視圖中提供csv鏈接,並將csv生成代碼放在ApplicationHelper
。 但是我收到了這個錯誤:
undefined method `send_data' for #<#<Class:0x0000010151c708>:0x0000010151a070>
引用這個:
send_data content, :type => "text/plain",
:filename => filename,
:disposition => 'attachment'
如果我將csv代碼放在控制器中它工作正常。 我希望使用幫助器來避免為每個我想為csv選項提供csv選項的控制器定義路由(我有一堆)。 如何讓send_data
可以使用send_data
(和其他必要的方法)?
helper_method
。 默認情況下, ApplicationController
中的方法只能在控制器內部訪問。
將一個方法添加到ApplicationController
並將其作為helper方法使用helper_method
:
class ApplicationController < ActionController::Base
helper_method :foo
def foo
"bar"
end
end
現在,控制器和視圖都可以訪問foo
方法。
如果問題是在所有控制器中使ApplicationHelper中的方法可用,為什么不添加一行
include ApplicationHelper
到ApplicationController文件?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.