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.
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
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.
精彩评论