Mongoid 错误“未定义方法‘删除’”对于:格式:符号”

发布于 2024-12-12 08:32:31 字数 3406 浏览 0 评论 0原文

我有 2 个嵌套模型:

class Interstate
  include Mongoid::Document
  field :name, :type => String
  field :url, :type => String
  field :time_zone, :type => String
  embeds_many :roadmaps
end

class Roadmap
  include Mongoid::Document
  field :title, :type => String
  field :privacy, :type => Integer
  field :interstate_id, :type => Integer
  embedded_in :interstate, :inverse_of => :roadmaps
end

我不确定 has_many|belongs_toembeds_many|embedded_in 之间的区别,但根据文档,最后一个对我来说更好,无论如何两者都没有不工作。此外,我还有嵌套资源:

 resources :interstates do
   resources :roadmaps
 end

当我尝试在州际公路中创建路线图时,会出现错误。

路线图控制器是标准脚手架,具有用于州际定义的 before_filter 方法:

 private
 def get_interstate
   @interstate = Interstate.find(params[:interstate_id])
 end

如果使用 embeds_many :roadmapsembedded_in :interstate 会发生其他错误:

NoMethodError in Roadmaps#new

Showing /home/alder/RubymineProjects/interstate/app/views/roadmaps/_form.html.haml where line #1 raised:

undefined method `delete' for :format:Symbol

Extracted source (around line #1):

1: = simple_form_for(@interstate, @roadmap) do |f|
2:   = f.error_notification
3: 
4:   .inputs

Trace of template inclusion: app/views/roadmaps/new.html.haml

Rails.root: /home/alder/RubymineProjects/interstate
Application Trace | Framework Trace | Full Trace

app/views/roadmaps/_form.html.haml:1:in `_app_views_roadmaps__form_html_haml___160176716082020505_38930380'
app/views/roadmaps/new.html.haml:3:in `_app_views_roadmaps_new_html_haml__4577296702858298215_34853620'
app/controllers/roadmaps_controller.rb:31:in `new'

控制台:

undefined method `delete' for :format:Symbol (ActionView::Template::Error)
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations.rb:80:in `notifying_parent?'                                                                                 
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations/embedded/remove.rb:30:in `block in persist'                                                                  
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/deletion.rb:23:in `prepare'                                                                                             
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations/embedded/remove.rb:29:in `persist'                                                                           
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence.rb:56:in `remove'                                                                                                       
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/actionpack-3.1.1/lib/action_view/helpers/form_helper.rb:392:in `apply_form_for_options!'                                                                      
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/actionpack-3.1.1/lib/action_view/helpers/form_helper.rb:365:in `form_for'

完整堆栈

所以,也许我应该添加一些选项来取消通知,但更有趣的是为什么格式是符号,我想它应该是一些模型对象。也许@interstate 不是模型。

此代码可以正确地与 sqlite db 一起使用。

I have 2 nested models:

class Interstate
  include Mongoid::Document
  field :name, :type => String
  field :url, :type => String
  field :time_zone, :type => String
  embeds_many :roadmaps
end

class Roadmap
  include Mongoid::Document
  field :title, :type => String
  field :privacy, :type => Integer
  field :interstate_id, :type => Integer
  embedded_in :interstate, :inverse_of => :roadmaps
end

I'm not sure about the difference between has_many|belongs_to and embeds_many|embedded_in, but according to documentations last is better for me, anyway both doesn't work. Also I have nested resources:

 resources :interstates do
   resources :roadmaps
 end

And Error appears when I'm trying to create Roadmap in Interstate.

Roadmap controller is standard scaffold with before_filter method for Interstate defining:

 private
 def get_interstate
   @interstate = Interstate.find(params[:interstate_id])
 end

In case with embeds_many :roadmaps and embedded_in :interstate other error occurs:

NoMethodError in Roadmaps#new

Showing /home/alder/RubymineProjects/interstate/app/views/roadmaps/_form.html.haml where line #1 raised:

undefined method `delete' for :format:Symbol

Extracted source (around line #1):

1: = simple_form_for(@interstate, @roadmap) do |f|
2:   = f.error_notification
3: 
4:   .inputs

Trace of template inclusion: app/views/roadmaps/new.html.haml

Rails.root: /home/alder/RubymineProjects/interstate
Application Trace | Framework Trace | Full Trace

app/views/roadmaps/_form.html.haml:1:in `_app_views_roadmaps__form_html_haml___160176716082020505_38930380'
app/views/roadmaps/new.html.haml:3:in `_app_views_roadmaps_new_html_haml__4577296702858298215_34853620'
app/controllers/roadmaps_controller.rb:31:in `new'

Console:

undefined method `delete' for :format:Symbol (ActionView::Template::Error)
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations.rb:80:in `notifying_parent?'                                                                                 
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations/embedded/remove.rb:30:in `block in persist'                                                                  
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/deletion.rb:23:in `prepare'                                                                                             
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence/operations/embedded/remove.rb:29:in `persist'                                                                           
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/mongoid-2.3.2/lib/mongoid/persistence.rb:56:in `remove'                                                                                                       
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/actionpack-3.1.1/lib/action_view/helpers/form_helper.rb:392:in `apply_form_for_options!'                                                                      
/home/alder/.rvm/gems/ruby-1.9.2-p290@global/gems/actionpack-3.1.1/lib/action_view/helpers/form_helper.rb:365:in `form_for'

Full stack

So, maybe I should add some options to cancel notifying, but more intresting why format is Symbol, I guess it should be Some Model Object. Perhaps @interstate is not the model.

This code works with sqlite db correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-12-19 08:32:31

问题是在圆括号中而不是方括号中。

simple_form_for [@interstate, @roadmap]

The issue was in round brackets instead of squared.

simple_form_for [@interstate, @roadmap]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文