0

我正在尝试在页面上创建添加评论表单,但它需要在同一页面上出现多次。

这是我的模型。我正在使用 Mongoid 3.0,但我认为这并不重要。模型工作正常。

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

我有这些路线

suggestion_comments POST   /suggestions/:suggestion_id/comments(.:format) comments#create
project_comments POST   /projects/:project_id/comments(.:format)       comments#create

我通过以下方式调用表格:

= render partial: "comments/new", locals: { :commentable => stream.project }
= render partial: "comments/new", locals: { :commentable => stream.suggestion }

如何创建 form_for?我试过这个,但它不起作用。此外,由于我需要在页面上多次显示此内容,因此在页面上id重复的 CSS 会导致此页面上的 JavaScript 出现问题。

= 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"
4

1 回答 1

0

经过大量尝试,我终于想通了......原来很简单:

= simple_form_for([commentable, Comment.new], :remote => true) do |f|
于 2012-07-30T10:54:10.740 回答