has_many :通过格式多选字段

发布于 2024-09-02 03:18:57 字数 1149 浏览 5 评论 0原文

我尝试使用 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-09-09 03:18:57

您在 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 an ActiveRecord::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 list to_label, display_name, full_name, name, title, username, login, value, to_s that returns the text you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文