belongs_to association and formtastic

时间:2013-06-09 23:12:18

标签: ruby-on-rails-3 formtastic belongs-to

我有一个模型Product,它与另一个模型Type有belongs_to关联。在产品的形式中,我使用formtastic来显示数据库中所有可用类型的选择标记,如下所示:

<%= f.input :type %> 

select正在显示OK,但它的每个选项都是Type模型的对象实例,作为字符串,例如:

#<Type:0x00eff180c85c8>

而不是那样,我想显示它的'title'属性,如:

Electronic
Domestic
...

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

尝试member_label选项,听起来就像你想要做的那样:

<%= f.input :type, :member_label => :title %>

documentation有更多例子。

答案 1 :(得分:1)

只需在模型中添加

即可
  def name
    return self.title
  end