我有两个模型:论坛应用程序中的帖子和图像,其中帖子使用 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
我发帖时遇到的错误:
undefined method
[]' 为#`
, 无方法错误
我现在不确定我在做什么或者这是从哪里来的。我不确定我的表单设置是否正确(我一直在关注类似的活动记录教程,但还没有深入研究 dm-accepts_nested 代码)。我可以通过命令行设置一些更基本的东西,但不能设置图像。我了解嵌套的基础知识,但并不真正了解如何将其集成到我正在做的自动取款机中。
也许有人知道。任何帮助表示赞赏。