has_many :通过格式多选字段
我尝试使用 has_many :through 方法建立多对多关系,然后使用多选字段来设置关系。我正在关注本教程:
http://asciicasts.com/episodes/185-formtastic -part-2
但是由于某种原因,表单显示了一个奇怪的十六进制数字,并且它改变了每次页面刷新,我不确定我做错了什么。下面是我的模型/视图代码。
company.rb
has_many :classifications
has_many :sics, :through => :classifications
sic.rbclassification.rb
has_many :classifications
has_many :companies, :through => :classifications
另外,
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
I'm trying to set up a many to many relationship using the has_many :through method and then use a multi-select field to setup the relationships. I'm following this tutorial:
http://asciicasts.com/episodes/185-formtastic-part-2
However for some reason the form displays a strange hex number and it changes each page refresh, I'm not exactly sure what I'm doing wrong. Below is my model/view code.
company.rb
has_many :classifications
has_many :sics, :through => :classifications
sic.rb
has_many :classifications
has_many :companies, :through => :classifications
classification.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 %>
Also here is the the form looks like it's showing the correct number of entries for the field but it is clearly not showing the correct name for the relationship.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
ActiveRecord::Base
对象的to_s
方法中看到的内容。十六进制数字是每个请求都会不同的内存位置。在研究了 Formastic 代码后,它会从预定义列表中查找方法来查找要显示的文本。
确保您的
Sic
模型在此列表to_label, display_name, full_name, name, title, username, login, value, to_s
中有一个返回文本的字段(或方法)你想要的。What you are seeing in the
to_s
method of anActiveRecord::Base
object. The hex number is the memory location which would be different each request.After poking around the Formastic code, it looks for methods from a predefined list to find the text to display.
Make sure your
Sic
model has a field (or method) in this listto_label, display_name, full_name, name, title, username, login, value, to_s
that returns the text you want.