【发布时间】:2011-07-14 03:58:02
【问题描述】:
我有密码
def showTags(post)
tagString = ''
tagString += "Politics " if post.tags(:politics)
tagString += "Technology " if post.tags(:technology)
tagString += "Entertainment " if post.tags(:entertainment)
tagString += "Sports " if post.tags(:sports)
tagString += "Science " if post.tags(:science)
tagString += "Crime " if post.tags(:crime)
tagString += "Business " if post.tags(:business)
tagString += "Social " if post.tags(:social)
tagString += "Nature " if post.tags(:nature)
tagString += "Other " if post.tags(:other)
return tagString
end
我的标签模型有 10 个布尔值(政治、技术等)和 belongs_to :post。当您发布新帖子时,会有对应于每个字段的复选框。因此,在发布帖子后,会有一些正确和错误的值。当我在 post#index 中调用此方法以显示哪些标签属于该帖子时,就会出现问题。问题是所有的单词都显示出来了,我用控制台玩了,post.tags 是正确的(一些是真值和一些假值)但是if post.tags(:politics)(或其他)总是返回真。我尝试使用if post.tags.politics,但这只是报错。
谁能帮帮我。
【问题讨论】:
标签: ruby-on-rails post tags