【发布时间】: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