使用dm-accepts_nested_attributes和dm-is-tree在Rails中嵌套2个模型的表单 [英] nested forms for 2 models in rails using dm-accepts_nested_attributes and dm-is-tree

查看:56
本文介绍了使用dm-accepts_nested_attributes和dm-is-tree在Rails中嵌套2个模型的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:论坛应用程序中的Post和Image,其中使用dm-is-tree以父子格式排列帖子.至此,这些图像已成为Post模型的一部分.由于Post模型变得笨拙,并且我需要增加更多深度来标记图像,因此我正在努力将Image拆分为自己的模型,但仍是输出中Post的一部分.

I have two models: Post and Image in a forum application where Posts are arranged in parent-child format using dm-is-tree. Up this point, the images had been part of the Post model. As the Post model gets unwieldy and I need to add more depth to notating the image, I'm working to spin off the Image into its own model, but is still part of the post in output.

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

So I started integrating dm-accepts_nested_attributes in a simple arrangement:

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)用于创建帖子:

I have this form(haml) on every page for creating a 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

, a NoMethodError

我现在不确定我在做什么或这是从哪里来的.我不确定我是否正确设置了表单(我一直在遵循类似的活动记录教程,并且还没有深入研究dm-accepts_nested代码).我可以通过命令行设置一些甚至更基本的内容,但不能设置图像.我了解嵌套的基础知识,但并不真正了解如何将其集成到我正在做的atm中.

I'm not sure what I'm doing or where this is coming from at this point. I'm not sure if I have the form set-up correctly (I've been following similar active record tutorials and haven't delved into the dm-accepts_nested code at all yet). I'm able to set some even more basic stuff through the command line, but not images. I understand the basics of the nesting, but not really how to integrate it to what I'm doing atm.

也许有人知道.任何帮助表示赞赏.

Maybe someone knows. Any help appreciated.

推荐答案

提交表单后得到的答复:

The response I get from submitting the form:

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"=>#<File:/tmp/RackMultipart20101222-874-bhvepi>}}, "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

我猜表格还可以,但是它不能保存图像.

I'm guessing the form is ok, but its not saving the image.

添加

@post.images_attributes = Image.new

或控制器的某些变体没有任何作用,所以我很好奇我是否需要在Post模型中创建某种挂钩来保存图像.我现在不知道.

or some variation to the controller does nothing so I'm curious if I need to create some sort of hook in the Post model to save the image. I do not know at this point.

这篇关于使用dm-accepts_nested_attributes和dm-is-tree在Rails中嵌套2个模型的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆