has_many:通过formtastic多选字段

Tri*_*eil 2 ruby-on-rails formtastic

我正在尝试使用has_many:through方法建立多对多关系,然后使用多选字段来设置关系.我正在学习本教程:

http://asciicasts.com/episodes/185-formtastic-part-2

但是由于某种原因,表单会显示一个奇怪的十六进制数字并且它会更改每个页面刷新,我不确定我做错了什么.下面是我的模型/视图代码.

company.rb

has_many :classifications
has_many :sics, :through => :classifications
Run Code Online (Sandbox Code Playgroud)

sic.rb

has_many :classifications
has_many :companies, :through => :classifications
Run Code Online (Sandbox Code Playgroud)

classification.rb

belongs_to :company
belongs_to :sic
Run Code Online (Sandbox Code Playgroud)

_form.html.erb

<% semantic_form_for @company do |f| %>
  <% f.inputs do %>
    <%= f.input :company %>
    <%= f.input :sics %>
  <% end %>
  <%= f.buttons %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

此外,表格看起来像是显示该字段的正确条目数,但显然没有显示该关系的正确名称.

SIC Multi-Select http://web9.twitpic.com/img/103694166-98ad71116216d3d1b12dd77690b36248.4bf6ca20-full.jpg

Sam*_*uel 6

你在对象的to_s方法中看到了什么ActiveRecord::Base.十六进制数是每个请求不同的内存位置.在探索Formastic代码之后,它会从预定义列表中查找要查找要显示的文本的方法.

确保您的Sic模型在此列表to_label, display_name, full_name, name, title, username, login, value, to_s中有一个字段(或方法),用于返回所需的文本.