2013-02-15 20 views
0

我在何處定義由多個控制器使用的幫助器方法時遇到了一些麻煩。未定義的方法`options_for_select'

我提取這種方法變成自己的模塊:

module ColumnMapHelper 
    def drop_down_upload_file_types 
    options = [["Use the RoyaltyZone Sales Upload Template", "RZ"], ["Use my own data file (Quickbooks, iTunes, etc)", "Create New"]] 
    ... 
    select_tag "file_upload_type", options_for_select(options, default), :id=>"upload_file_type_selection" 
    end 
end 

我把它在控制器和暴露它作爲一個輔助方法:

class SalesDataController < ApplicationController 
    before_filter :login_required, :except => [:download] 

    include ColumnMapHelper 
    helper_method :drop_down_upload_file_types 

當它在我的控制器的助手定義( app/helpers/sales_data_helper.rb)這很好,但現在我得到這個錯誤,當我的幫助方法在視圖中調用時:

undefined method `options_for_select' for #<SalesDataController:0x109bbbd18> 

我需要包含哪些模塊?分享這樣的傭工的最佳方式是什麼?

+0

是否有理由將其包含在控制器中? – 2013-02-15 23:31:06

+0

我曾經在某處讀過ApplicationController中的共享助手方法。 – 2013-02-17 14:49:23

回答

0

options_for_select是一種視圖方法,它在控制器中不可用。但是,沒有理由在控制器中使用它。

如果將ColumnMapHelper文件保存在/app/helpers文件夾中,則drop_down_upload_file_types將自動適用於所有視圖(只要您的應用默認包含所有幫助程序)。

+0

它是全球性的嗎?沒有名稱空間前綴? – 2013-02-17 14:50:13

+0

是的,只要你在視野中。 – 2013-02-17 17:55:15

1

如果你只在你的視圖中使用助手,那麼你不需要做任何事情,因爲app/helpers中的所有內容都是默認包含的。

當您想要與視圖共享current_user之類的方法時,您希望在控制器中使用helper_method。