Rails4多态如何在html页面中使用?

时间:2018-06-10 12:09:41

标签: ruby-on-rails polymorphic-associations

我使用了Rails-4.2.8。

现在我有question article comments个模型,有问题有很多评论,或者有一篇文章有​​很多评论,但现在我怎么能在comment中提交question {1}} show.html.erb

我的models如下:

#comment.rb
class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
end

#question.rb
class Question < ActiveRecord::Base
   has_many :comments, :as => :commentable, :dependent => :destroy
 end

我的question show.html.erb如下:

<% @comment = @question.comments.build  %>
<%= form_for([@comment.commentable,@comment]) do |f| %>
    <%= f.text_area :content,class: "editormd-markdown-textarea" %>
    <%= f.submit "submit", class: "btn btn-primary pull-right" %>
<% end %>

我的questions_controller.rb如下:

def create
   Question.find(params[:question_id]).comments.build
end

我的roots.rb如下

resources :questions do
    resources :comments
  end

但是我得到了这个错误,有人可以给我一些建议,我的错误代码在哪里?非常感谢!

Routing Error
uninitialized constant CommentsController

enter image description here

还有:

Request
Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"/MIczvX+UVpMnKx9K2/7cTmoiM1oDcYZO63RE10mDAlnRMJRukFq/2t24yBqfDIg8D1/Midk3FU1IOaTW4twuA==",
 "comment"=>{"content"=>"adfadfadfadfadfdafadf"},
 "comment-editormd-html-code"=>"<p>adfadfadfadfadfdafadf</p>\r\n",
 "commit"=>"Submit",
 "question_id"=>"1"}

1 个答案:

答案 0 :(得分:0)

尝试修复

<%= form_for [@comment.commentable,@comment], url: "#{polymorphic_path(commentable)}/comments" do |f| %>