I’m trying to create an add comment form on a page, but it’ll need to appear multiple times on the same page.
Here’s my model. I’m using Mongoid 3.0, but I don’t think that matters. The models are working correctly.
class Project
include Mongoid::Document
...
embeds_many :comments, :as => :commentable
...
end
class Suggestion
include Mongoid::Document
...
embeds_many :comments, :as => :commentable
...
end
class Comment
include Mongoid::Document
embedded_in :commentable, :polymorphic => true
...
end
I have these routes
suggestion_comments POST /suggestions/:suggestion_id/comments(.:format) comments#create
project_comments POST /projects/:project_id/comments(.:format) comments#create
I’m calling the form via:
= render partial: "comments/new", locals: { :commentable => stream.project }
= render partial: "comments/new", locals: { :commentable => stream.suggestion }
How do I create the form_for? I tried this but it doesn’t work. Also, since I need to display this on the page multiple times, having a CSS id that duplicates on the page will cause issues with the JavaScript on this page.
= form_for commentable.comments, remote: true do |f|
%fieldset
.control-group
.controls
= f.text_area :content, :required => true, :placeholder => "Add a comment...", :maxlength => "1000"
.form-actions
= f.submit "Post"
After a lot of trying, I finally figured it out… Turns out to be quite simple: