2011-09-15 52 views
2

我在user_helper.rb,當我在如何訪問我在Rails中編寫的幫助程序?

<div class="field"> 
     <%= f.label :assignee, "Assigned to" %> 
     <%= select(:task, :assignee_id, User.get_array_of_names_and_user_ids)%> 
    </div> 

鍵入它不能看到它寫一個幫手我的用戶模型

module UserHelper 
    def get_array_of_names_and_user_ids 
    User.all(&:first_name) + User.all.map(&:user_id) 
    end 
end 

可惜。我哪裏錯了?我正在使用設計。

回答

4

你很近。幫助程序不會成爲這樣的類方法 - 它可以作爲視圖中的方法訪問。只需撥打get_array_of_names_and_user_ids即可。

0

助手是可以在視圖中調用的方法,而不是方法被稱爲型號 只需撥打get_array_of_names_and_user_ids

2

助手是沒有意見的模型。 對於模型應該定義在用戶模式

class User 
    def self.get_array_of_names_and_user_ids 
    User.all(&:first_name) + User.all.map(&:user_id) 
    end 
end 
1

類方法不需要手工編寫這個助手爲Rails提供一個用於此目的的所謂collection_select幫手。

我假設你有一個小的用戶組中的DB(< 30):

在你看來只是補充一點:

<%= collection_select(:task, :assignee_id, User.all, :id, :first_name, 
    :prompt => true) %> 

注意。否則,您必須使用其他控件來選擇用戶。

相關問題