【问题标题】:Issue with collection_select / mongoid / mongodbcollection_select / mongoid / mongodb 的问题
【发布时间】:2011-06-16 23:08:11
【问题描述】:

我在使用 mongoid 的 rails 项目时遇到了一些问题。

问题在于创建/编辑新帖子时。

我想为帖子选择一个主题/类别(娱乐/新闻/ 杂项)。

起初我尝试使用 railscast 238(接近尾声)中的方法 关键价值

http://railscasts.com/episodes/238-mongoid

但我不断收到 BSON ObjectID 错误。在寻找一个 解决方案我找到了讨论它的主题,但提到的修复是 已经在我正在使用的 mongoid 版本中。

所以我改变了。我不知道这是好习惯还是 不是,但我没有想法。而不是 railscast 方法,我使用 下面的代码。但是,我收到此错误...

“未定义的方法‘元数据’ "4d4165b3fcf1ee14e0000049":字符串"

post model

class Post
  include Mongoid::Document
  field :link
  field :title
  field :synopsis
  field :added_on, :type => Date

  validates_presence_of :link

  embeds_many :replies
  embeds_one :topic
end

topic model

class Topic
  include Mongoid::Document
  field :category, :type => String

  embedded_in :post, :inverse_of => :topics
end

 

_form.html.erb
        <div class="field">
                <%= f.label :topic_id %>
                <%= f.collection_select :topic, Topic.all, :id, :category, :prompt
=> "Select a Topic" %>
        </div>

我最近尝试从embedded_in/embeds_one 更改为references_one/referenced_in。我也被告知“Topic.all”是错​​误的,但我不知道如何改变它才能让它工作。

Gemfile 信息...

gem "mongoid", "2.0.0.rc.6"
gem "bson_ext", "~> 1.2"

有什么突出的吗?

编辑:

更新到2.0.0.rc.7还是拿不到。

为了好玩,尝试了 railscast 视频中的关键方法。相同的 “PostsController#update 中的 BSON::InvalidObjectId”错误。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 mongodb mongoid


    【解决方案1】:

    按照下面链接中的示例进行操作,该示例显示了如何将 embeds_one 与嵌套表单一起使用 mongoid nested form with embeds_one

    【讨论】:

      【解决方案2】:

      在您的课程主题中,它应该是 embedded_in :post, :inverse_of => :topic 你有 :inverse_of => :topics 但你使用 embeds_one :topic 不多

      【讨论】:

      • @user650437 我之前尝试过这样做,但没有成功。最终我不得不放弃使用嵌入式并切换到引用。不知道这是好是坏,但这是我让它工作的唯一方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多