使用dm-accepts_nested_attributes和dm-is-tree在rails中的2个模型的嵌套表单

我有两个模型:论坛应用程序中的post和图像,其中post使用dm-is-tree以父子格式排列。 在这一点上,图像已成为Post模型的一部分。 由于Post模型变得笨拙并且我需要添加更多深度来表示图像,我正在努力将Image分离到它自己的模型中,但仍然是输出中的post的一部分。

所以我开始以简单的方式集成dm-accepts_nested_attributes:

class Post include DataMapper::Resource property :id, Serial property :istop, String property :created_at, DateTime property :updated_at, DateTime property :content, Text has n, :images accepts_nested_attributes_for :images is :tree, :order => [:istop, :created_at] class Image include DataMapper::Resource property :id, Serial property :created_at, DateTime belongs_to :post property :image, String, :auto_validation => false # Carrierwave image info mount_uploader :image, ImageUploader # Carrierwave uploader 

我在每个页面上都有这个表单(haml)来创建post:

  = form_for [@forum,Post.new], :html => {:multipart => true} do |f| = f.hidden_field :istop, :value => "parent" = f.text_area :content = f.fields_for :simages_attributes do |g| = g.file_field :image .actions = f.submit 

那个控制器:

 def create @forum = Forum.get(params[:forum_id]) @post = @forum.posts.create(params[:post]) respond_to do |format| if @post.save format.html { redirect_to(forum_path(@forum), :notice => 'Post was successfully created.') } else format.html { redirect_to(forum_path(@forum), :notice => 'There was an error in posting') } end end end 

我发布时收到的错误:

对于#`, undefined method []’

,NoMethodError

我不确定我在做什么,或者在这一点上来自哪里。 我不确定我是否正确设置了表单(我一直在关注类似的活动记录教程,并且还没有深入研究dm-accepts_nested代码)。 我可以通过命令行设置一些更基本的东西,但不是图像。 我理解嵌套的基础知识,但不知道如何将它整合到我正在做的事情中。

也许有人知道。 任何帮助赞赏。

attr_accessor:Post模型中的images_attributes,允许表单提交

但是,图像现在没有被保存,即在某处丢失并且未保存

我从提交表单中得到的回复:

 Started POST "/forums/x/posts" for 127.0.0.1 at 2010-12-22 10:15:19 -0500 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"/cyeRIls9M..7U8eG1lXAJg8=", "post"=>{"istop"=>"parent", "content"=>"sdfgsdfg", "images_attributes"=>{"image"=>#}}, "commit"=>"Create Shout", "forum_id"=>"x"} SQL (0.054ms) SELECT "name" FROM "forums" WHERE "name" = 'x' ORDER BY "name" LIMIT 1 SQL (115.419ms) INSERT INTO "posts" ("istop", "created_at", "updated_at", "forum_name") VALUES ('parent', '2010-12-22T10:15:20-05:00', '2010-12-22T10:15:20-05:00', '', 'sdfgsdfg', 0, 'x') Redirected to http://localhost:3000/forums/x Completed 302 Found in 123ms 

我猜这个表格没问题,但它没有保存图片。

添加

 @post.images_attributes = Image.new 

或者控制器的某些变化什么也没做,所以我很好奇我是否需要在Post模型中创建某种钩子以保存图像。 我现在还不知道。