Rails:如何使用activeRecord在formtastic中选择

时间:2010-08-17 17:09:03

标签: ruby-on-rails select formtastic

我是铁杆新手,我想你可以轻松回答这个问题。

到目前为止我得到的是

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

其中任务集合由

定义
Task.find(:all)

在控制器内。

这确实有效,向我显示了一个下拉列表,列出了要从中选择并连接它们的所有任务。 这里的问题是,下拉菜单显示了像

这样的值
#<Task:0x123456789d1234>

我在哪里定义显示的值?

2 个答案:

答案 0 :(得分:4)

我相信您可以使用:label_method来解决您的问题...

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

其中:title是您想要展示的内容。

This可能会有所帮助。

答案 1 :(得分:0)

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

class Task < ActiveRecord::Base

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

end