问题
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