草庐IT

mongodb - Mongoid embeds_many/embedded_in 与 ActiveAdmin 中的 Formtastic 的关系......?

coder 2023-10-26 原文

哇 - 许多项目都包含在这个项目中,我做了一些(广泛的)搜索但无济于事,所以抛出一个 flare 看看是否有其他人正在使用类似的堆栈并有解决方案。

我正在使用 Mongoid-enabled fork of ActiveAdmin为 Rails 3 应用构建管理界面。

ActiveAdmin(它使用 Formtastic 构建其表单)到目前为止似乎正在发挥作用。但是在尝试将此堆栈与 Mongoid 的 embeds_manyembedded_in 关系一起使用时,我遇到了麻烦。我正在努力成为一个好公民,并在 Mongo 中使用适当的数据建模技术,但 Formtastic 似乎不想配合。

我将分享我遇到的具体错误,尽管它可能很深奥。

如果我不能找到一个好的解决方案,我会接受它并只使用 has_manybelongs_to,即使我知道我会给出介绍 MongoDB 的主要优势(该应用程序不够大,因此缺少连接/多个查询会成为一个问题,但我想我会从一开始就以正确的方式做事!)。

class Location
  include Mongoid::Document
  embeds_many :events
  field :venue_name, type: String
end

class Event
  include Mongoid::Document
  embedded_in :location
  field :event_name, type: String
end

ActiveAdmin.register Event do
  form do |f|
    f.inputs do
      f.input :event_name
      f.input :location, :as => :select
    end
  f.buttons
end

它在 f.input :location 行上窒息并抛出:

ActionView::Template::Error (undefined method `event_id' for #<Event:0x007fa4224a20e0>):
1: insert_tag renderer_for(:new)

我将 :as => :select 更改为 :as => :check_boxes 并且实际上让它显示正确的位置(虽然是复选框,而不是选择或 radio )。但是在提交表格后,我收到了:

Mongoid::Errors::NoParent (
  Problem:
  Cannot persist embedded document Event without a parent document.
)

在查看原始提交时,它试图将我的选择作为“event_id”而不是父文档(位置)的 ID 发送。

无论如何 - 我的直觉是我正在尝试将方形钉子装入此处的圆孔中,但如果其他人有任何想法,他们将不胜感激。

最佳答案

错误说明了一切。如果没有先创建父项,则无法创建嵌入记录。

是的,我认为在您的情况下 has_many 和 belongs_to 关系将是正确的选择。

关于mongodb - Mongoid embeds_many/embedded_in 与 ActiveAdmin 中的 Formtastic 的关系......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13499212/

有关mongodb - Mongoid embeds_many/embedded_in 与 ActiveAdmin 中的 Formtastic 的关系......?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  3. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  6. ruby - inverse_of 是否适用于 has_many? - 2

    当我使用has_one时,它​​工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290

  7. ruby - Sinatra set cache_control to static files in public folder编译错误 - 2

    我不知道为什么,但是当我设置这个设置时它无法编译设置:static_cache_control,[:public,:max_age=>300]这是我得到的syntaxerror,unexpectedtASSOC,expecting']'(SyntaxError)set:static_cache_control,[:public,:max_age=>300]^我只想将“过期”header设置为css、javaascript和图像文件。谢谢。 最佳答案 我猜您使用的是Ruby1.8.7。Sinatra文档中显示的语法似乎是在Ruby1.

  8. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  9. ruby-on-rails - rails : check if the model was really saved in after_save - 2

    ActiveRecord用于在每次调用保存方法时调用after_save回调,即使模型没有更改并且没有生成插入/更新查询也是如此。这实际上是默认行为。在大多数情况下这没问题。但是一些after_save回调对模型是否实际保存的事情很敏感。有没有办法确定模型是否实际保存在after_save中?我正在运行以下测试代码:classStage 最佳答案 ActiveRecordusetocallafter_savecallbackeachtimesavemethodiscalledevenifthemodelwasnotchangedan

  10. ruby-on-rails - ruby 范围 : operators in case statement - 2

    我想检查my_number是否在某个范围内,包括较高的值。在IF语句中我会简单地使用“x>100&&x但是我应该在Ruby案例中做什么(开关)?使用:casemy_numberwhenmy_number不起作用。备注:标准范围不包括my_number恰好为500的情况,并且我不想添加第二个“when”,因为我必须编写双重内容casemy_number#between100and500when100..500puts"Correct,dosomething"when500puts"Correct,dosomethingagain"end 最佳答案

随机推荐