我使用了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
还有:
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"}
答案 0 :(得分:0)
尝试修复
<%= form_for [@comment.commentable,@comment], url: "#{polymorphic_path(commentable)}/comments" do |f| %>