Rails 中使用 dm-accepts_nested_attributes 和 dm-is-tree 的 2 个模型的嵌套形式
我有两个模型:论坛应用程序中的帖子和图像,其中帖子使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
我从提交表单得到的响应:
我猜表单没问题,但它没有保存图像。
向控制器添加
或某些变化没有任何作用,所以我很好奇是否需要在 Post 模型中创建某种挂钩来保存图像。我现在不知道。
The response I get from submitting the form:
I'm guessing the form is ok, but its not saving the image.
Adding
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.