belongs_to association and formtastic

£可爱£侵袭症+ 提交于 2019-12-24 11:53:25

问题


I have a model Product which has a belongs_to association with another model Type. In the product's form, I'm using formtastic to display a select tag with all the types available in the database, like this:

<%= f.input :type %> 

The select is showing up OK, but each option of it is an object instance of the Type model as a string, for example:

#<Type:0x00eff180c85c8>

Instead of that, I'd like to display the 'title' attribute of it, like:

Electronic
Domestic
...

Any ideas?


回答1:


Try the member_label option, it sounds like what you want to do:

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

The documentation has more examples.




回答2:


Simply add this in your model

  def name
    return self.title
  end


来源:https://stackoverflow.com/questions/17015084/belongs-to-association-and-formtastic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!