我有两个嵌套模型:
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_to
和embeds_many|embedded_in
之间的区别,但根据文档记录最后对我来说更好,无论如何两者都不起作用。我也有嵌套资源:
resources :interstates do
resources :roadmaps
end
当我试图在州际公路上创建路线图时会出现错误。
路线图控制器是标准脚手架,具有用于Interstate定义的before_filter方法:
private
def get_interstate
@interstate = Interstate.find(params[:interstate_id])
end
如果embeds_many :roadmaps
和embedded_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'
所以,也许我应该添加一些选项来取消通知,但更有趣的是为什么格式是Symbol,我想它应该是一些模型对象。也许@interstate
不是模特。
此代码可以正确使用sqlite db。
答案 0 :(得分:3)
问题出现在圆括号中而不是平方。
simple_form_for [@interstate, @roadmap]