【发布时间】:2010-11-09 02:30:46
【问题描述】:
我正在开发一个待办事项应用程序,以练习使用 Rails3 和 MongoDB。
我有一个项目,它是一个标签,一个标签将有许多与之关联的项目。
我的问题:show.html.erb 不想显示标签名称。
# models/item.rb
class Item
include Mongoid::Document
field :name
field :completed, :type => Boolean
referenced_in :tag
end
# models/tag.rb
class Tag
include Mongoid::Document
field :name
key :name
references_many :items
end
# items_controller.rb
def show
@item = Item.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @item }
end
end
<!-- items/show.html.erb -->
<p>Name: <%= @item.name %></p>
<p>Completed: <%= @item.completed %></p>
<p>Tag: <%= @item.tag.name %></p>
显示文件的第三行或标记行是我的问题所在。错误如下:
找不到类标签的文档 ID 4cd75765f5c4932f19000002。
如果我用下面的行替换有问题的行,则会显示正确的标签 ID(但我显然想要标签名称)
<p>Tag: <%= @item.tag_id %></p>
【问题讨论】:
-
你从
@item.tag得到什么?
标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid