Featured post
ruby - nested forms for 2 models in rails using dm-accepts_nested_attributes and dm-is-tree -
i have 2 models: post , image in forum application posts arranged in parent-child format using dm-is-tree. point, images had been part of post model. post model gets unwieldy , need add more depth notating image, i'm working spin off image own model, still part of post in output.
so started integrating dm-accepts_nested_attributes in 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 :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 form(haml) on every page creating post:
= form_for [@forum,post.new], :html => {:multipart => true} |f| = f.hidden_field :istop, :value => "parent" = f.text_area :content = f.fields_for :simages_attributes |g| = g.file_field :image .actions = f.submit
that goes controller:
def create @forum = forum.get(params[:forum_id]) @post = @forum.posts.create(params[:post]) respond_to |format| if @post.save format.html { redirect_to(forum_path(@forum), :notice => 'post created.') } else format.html { redirect_to(forum_path(@forum), :notice => 'there error in posting') } end end end
the error when posting:
undefined method
[]' #`
, nomethoderror
i'm not sure i'm doing or coming @ point. i'm not sure if have form set-up correctly (i've been following similar active record tutorials , haven't delved dm-accepts_nested code @ yet). i'm able set more basic stuff through command line, not images. understand basics of nesting, not how integrate i'm doing atm.
maybe knows. appreciated.
attr_accessor :images_attributes in post model, allows form submit
however, image not being saved, i.e. gets lost somewhere , isn't saved
- Get link
- X
- Other Apps
Comments
Post a Comment