Stumped by ActiveRecord controller error
I'm making a message board application. Users can make a post, each post requires a tag. Users can comment on the posts. Pretty simple. I've been hacking away on it and got an error I can't explain. I made a post, message#index shows the the list of posts including the newest one. The title of each post links to the message#show view (nothing special here) and the 24 other posts on message#index can be clicked on to visit their associated message#show. But not this recent one. I get the following error when I visit the message#show of the offending 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_recor开发者_如何学Cd/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])
I really don't understand what's going on here. Can someone enlighten me? I'll post any other needed code. Thank you for reading my question.
The problem is that you're looking up the tag using the message id. In the messages#show
action params[:id]
is the the id of the Message
model, not the tag.
Assuming that Tag
is an association of Message
such as has_one :tag
or has_many :tags
then you can get a message tag as using:
@message = Message.find(params[:id])
@tag = @message.tag # has_one
or
@tags = @message.tags # has_many
精彩评论