【问题标题】:nested forms for 2 models in rails using dm-accepts_nested_attributes and dm-is-tree使用 dm-accepts_nested_attributes_for 和 dm-is-tree 在 Rails 中嵌套 2 个模型
【发布时间】:2010-12-20 23:37:51
【问题描述】:

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

所以我开始以简单的方式集成 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)用于创建帖子:

 = 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 代码)。我可以通过命令行设置一些更基本的东西,但不能设置图像。我了解嵌套的基础知识,但并不真正了解如何将它集成到我正在做的 atm 中。

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

【问题讨论】:

    标签: ruby-on-rails ruby datamapper ruby-on-rails-3 nested-forms


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      提交表单后得到的回复:

      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
      

      我猜表格没问题,但它没有保存图像。

      添加

      @post.images_attributes = Image.new
      

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

      【讨论】:

        猜你喜欢
        • 2011-10-07
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 2021-11-27
        • 2020-06-15
        • 2015-05-14
        相关资源
        最近更新 更多