2

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

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

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

公司.rb

has_many :classifications
has_many :sics, :through => :classifications

sic.rb

has_many :classifications
has_many :companies, :through => :classifications

分类.rb

belongs_to :company
belongs_to :sic

_form.html.erb

<% semantic_form_for @company do |f| %>
  <% f.inputs do %>
    <%= f.input :company %>
    <%= f.input :sics %>
  <% end %>
  <%= f.buttons %>
<% end %>

此外,表单看起来显示了正确的字段条目数,但显然没有显示关系的正确名称。

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

4

1 回答 1

6

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

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

于 2010-05-21T18:19:12.613 回答