被ActiveRecord控制器错误所困扰

时间:2010-07-31 07:49:38

标签: ruby-on-rails activerecord controller

我正在制作留言板申请表。用户可以发帖,每个帖子都需要一个标签。用户可以对帖子发表评论。很简单。我一直在攻击它并得到一个我无法解释的错误。我发了帖子,消息#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])

我真的不明白这里发生了什么。有人可以开导我吗?我会发布任何其他所需的代码。感谢您阅读我的问题。

1 个答案:

答案 0 :(得分:1)

问题是您正在使用消息ID查找标记。在messages#show操作中params[:id]Message模型的ID,而不是标记。

假设TagMessage的关联,例如has_one :taghas_many :tags,那么您可以使用以下内容获取消息代码:

@message = Message.find(params[:id])
@tag = @message.tag # has_one

@tags = @message.tags # has_many