Reputation: 579
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_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])
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.
Upvotes: 0
Views: 167
Reputation: 7477
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
Upvotes: 1