无法在 ruby on Rails 上显示 actions_as_taggable_on 标签(并且格式非常好!)
给定
作为用户,我位于嵌套的新供应商/5/评论/新中。除了将写入 Review 模型的 :params 之外,我还需要能够包含属于 Vendor 模型的标签。
我已经使用了acts_as_taggable_on(http://github.com/mbleigh/acts-as-taggable -on):
class Vendor....
acts_as_taggable_on :tags, :competitors
我使用 formtastic 提交标签和 field_for 以确保我写信给供应商,即使表单位于 CREATE 上 评论:
semantic_form_for ....
<% fields_for :vendor do |vendor| %>
<p>
<%= vendor.label :tags %><br />
<%= vendor.text_field :tag_list %>
</p>
<% end %>
我尝试使用以下内容显示供应商的标签:
Tags: <%=h @vendor.tag_list %>
我的结果: 没有什么。
1)我写的标签正确吗?它看起来就像在控制台中创建了正确的SQL
2) 我是否正在使用acts_as_taggable_on 采取正确的方法来显示标签列表?
Given
As the User, I am at a nested new vendors/5/reviews/new. In addition to :params that will get written to the Review model, I need to be able to include tags that belong to the Vendor model.
I have used acts_as_taggable_on (http://github.com/mbleigh/acts-as-taggable-on):
class Vendor....
acts_as_taggable_on :tags, :competitors
I use formtastic to submit the tags and field_for to make sure that I write to vendor even though the form is on a CREATE Review:
semantic_form_for ....
<% fields_for :vendor do |vendor| %>
<p>
<%= vendor.label :tags %><br />
<%= vendor.text_field :tag_list %>
</p>
<% end %>
I try to display the tags for the Vendor with the following:
Tags: <%=h @vendor.tag_list %>
My outcome: NOTHING.
1) Am I correctly writing the tags? It looks like it is create the right SQL in the console
2) Am I doing the right approach to display the tag list using acts_as_taggable_on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我有点困惑为什么你要使用 Formtastic (semantic_form_for),因为表单中的所有助手都是标准的 Rails 助手(fields_for、label、text_field),所以这实际上与 Formtastic 没有太大关系。
其次,如果表单用于供应商记录 (form_for(@vendor)),则 fields_for(:vendor) 没有任何意义。在 form_for 中使用 fields_for 创建具有嵌套属性的嵌套表单(可用于同时创建父记录和关联记录)。
我需要查看表单代码的完整示例才能真正掌握您想要做的事情,但我认为您已经将一些非常简单的事情变得过于复杂了。不管怎样,我的建议是在使用semantic_form_for之前正确理解form_for。
First, I'm a bit confused why you're using Formtastic (semantic_form_for) when all of the helpers in the form are standard rails helpers (fields_for, label, text_field), so this really doesn't have much to do with Formtastic.
Second, if the form is for a Vendor record (form_for(@vendor)), then fields_for(:vendor) doesn't make any sense. Using fields_for inside a form_for creates a nested form with nested attributes (useful to create a parent record and an associated record at the same time).
I'd need to see a complete sample of the form code to really get a grip on what you're trying to do, but I think you've over complicated something quite simple. Either way, my advice is to correctly understand form_for before using semantic_form_for.