2010-08-17 31 views
0

我是新來的rails,我想你可以很容易地回答這個問題。Rails:如何在formtastic中使用select with activeRecord

我走到這一步,是

= f.input :task, :as => :select, :collection => @tasks, :include_blank => true 

其任務集合由

Task.find(:all) 

內的控制器定義。

這實際上工作,顯示我的所有任務的下拉列表中選擇並連接它們。 這裏的問題是,在下拉菜單中顯示我像重視

#<Task:0x123456789d1234> 

在哪裏定義正在顯示什麼樣的價值?

回答

4

我相信你可以使用:label_method爲您解決問題...

f.input :task, :as => :select, :collection => @tasks, 
    :include_blank => true, :label_method => :title 

其中:title是要顯示的內容。

This可能會有所幫助。

+0

非常感謝你mouch,您發佈的鏈接偉大的工作。 只需在模型類中實現「to_label」函數 – Infinite 2010-08-17 19:09:45

0

您可以在模型中定義to_s方法:

class Task < ActiveRecord::Base 

    def to_s 
    title # the attribute to display for the label 
    end 

end 
相關問題