我目前有一个带有以下代码的标记系统
rails g model tag name:string
rails g model tagging post_id:integer tag_id:integer
rake db:migrate
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
end
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
validates_presence_of :name, :content
attr_writer :tag_names
after_save :assign_tags
def tag_names
@tag_names || tags.map(&:name).join(' ')
end
private
def assign_tags
if @tag_names
self.tags = @tag_names.split(/\s+/).map do |name|
Tag.find_or_create_by_name(name)
end
end
end
end
<p>
<%= f.label :tag_names %><br />
<%= f.text_field :tag_names %>
</p>
在 _post.html.erb 部分
我正在尝试通过执行调用具有特定标签的帖子
<%= post.find_by_tag("sports") %>
当我这样做时,所有带有标签的帖子都会出现,这是我想要的,但它们以这种格式出现
[#<Post id: 11, title: "adfads", body: "adsfasd", user_id: 1, created_at: "2012-05-30 06:17:52", updated_at: "2012-05-30 06:17:52", votes_count: 1>, #<Post id: 8, title: "Cover Letter", body: "Dear Human Resources:\r\n\r\nI would like to make mysel...", user_id: 1, created_at: "2012-05-30 00:28:56", updated_at: "2012-05-30 00:28:56", votes_count: 1>]
我怎样才能让它们以更令人愉快的帖子标题帖子正文的方式出现,而不是现在呈现的内容?