Rails 中使用 dm-accepts_nested_attributes 和 dm-is-tree 的 2 个模型的嵌套形式

发布于 2024-10-08 17:21:29 字数 2037 浏览 3 评论 0原文

我有两个模型:论坛应用程序中的帖子和图像,其中帖子使用 dm-is-tree 以父子格式排列。到目前为止,这些图像已经成为 Post 模型的一部分。由于 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)用于创建帖子:

 = 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

发布时出现的错误:

未定义的方法[ ]' for #`

,一个 NoMethodError

我不确定我在做什么,也不知道这是从哪里来的。我不确定我的表单设置是否正确(我一直在关注类似的活动记录教程,但尚未深入研究 dm-accepts_nested 代码)。我可以通过命令行设置一些更基本的东西,但不能设置图像。我了解嵌套的基础知识,但并不真正了解如何将其集成到我正在做的自动取款机中。

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

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.

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

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

That goes to this controller:

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

The error I get when posting:

undefined method[]' for #`

, a NoMethodError

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.

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

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

发布评论

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

评论(2

避讳 2024-10-15 17:21:29

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

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

attr_accessor :images_attributes in the Post model, allows the form to submit

However, the image is now not being saved, i.e. gets lost somewhere and isn't saved

你是我的挚爱i 2024-10-15 17:21:29

我从提交表单得到的响应:

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 模型中创建某种挂钩来保存图像。我现在不知道。

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.

Adding

@post.images_attributes = Image.new

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.

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