Rails 打印标签名称时标签失败

Rails tagging fail when printing the name of the tag

我正忙于在 RoR 中构建标记系统。 这个系统我已经编码根据:http://tutorials.jumpstartlab.com/projects/blogger.html#i3:-tagging 现在,当我打印出 tag.name 时,它可以工作,但我也得到了看起来像整个 tag_list 的东西,作为一个直接打印在它旁边的对象。我浏览器中的视图渲染阐明了我的问题:

#

Post

标签:[ tag1 tag2 tag3 [#, #, #]]

每个括号实际显示为:Tag id: 27, name: "tag1", created_at: "2015-05-12 09:24:02", updated_at: “2015-05-12 09:24:02”> ... 三倍(在本例中。)

#

(不知何故 Whosebug 没有显示我在浏览器中处理的混乱的完整性,而只是显示括号。)

... 作为 show.html.haml 中这段代码的直接结果:

Tags: [
= @post.tags.each do |tag|
    = link_to tag.name, tag_path(tag)
]

我希望它简单地像这样:

#

Post

标签:[ tag1 tag2 tag3 ]

#

甚至没有括号。没关系。 我的 post.rb 模型如下所示:

    has_many :taggings
    has_many :tags, through: :taggings

    def tag_list=(tags_string)
        tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
        new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) }
        self.tags = new_or_found_tags
    end

    def tag_list
        self.tags.collect do |tag|
          tag.name
        end.join(", ")
    end

我的 routes.rb 看起来像这样:

Rails.application.routes.draw do
  root 'posts#index'

  resources :posts do
    resources :comments
  end
  resources :tags
end

我知道有用于标记的 gem,但对我来说,完全控制此后开发的标记非常重要。 标签最终将通过复杂的 javascript 可视化库。 这可能是非常小的东西。感谢您的帮助!

替换:

= @post.tags.each do |tag|
    = link_to tag.name, tag_path(tag)

与:

- @post.tags.each do |tag|
    = link_to tag.name, tag_path(tag)

= 打印集合