我正在制作留言板申请表。用户可以发帖,每个帖子都需要一个标签。用户可以对帖子发表评论。很简单。我一直在攻击它并得到一个我无法解释的错误。我发了帖子,消息#index显示的帖子列表包括最新的帖子。每个帖子的标题链接到消息#show view(这里没什么特别的)和消息#index的其他24个帖子可以点击访问他们的相关消息#show。但不是最近的那个。当我访问消息#shord of the offnding post:
时,我收到以下错误Couldn't find Tag with ID=131
...
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
/home/vvlist/website/app/controllers/messages_controller.rb:20:in `show'
messages_controller.rb:20:
@tag = Tag.find(params[:id])
我真的不明白这里发生了什么。有人可以开导我吗?我会发布任何其他所需的代码。感谢您阅读我的问题。
答案 0 :(得分:1)
问题是您正在使用消息ID查找标记。在messages#show
操作中params[:id]
是Message
模型的ID,而不是标记。
假设Tag
是Message
的关联,例如has_one :tag
或has_many :tags
,那么您可以使用以下内容获取消息代码:
@message = Message.find(params[:id])
@tag = @message.tag # has_one
或
@tags = @message.tags # has_many