Ruby on rails Rails4多态如何在html页面中使用?
我使用了Rails-4.2.8 现在我有Ruby on rails Rails4多态如何在html页面中使用?,ruby-on-rails,polymorphic-associations,Ruby On Rails,Polymorphic Associations,我使用了Rails-4.2.8 现在我有问题文章评论模型,关于问题有很多评论,或者一篇文章有很多评论,但是现在我如何在问题show.html.erb中提交评论 我的型号如下: #comment.rb class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end #question.rb class Question < ActiveRecord::Base h
问题
文章
评论
模型,关于问题有很多评论,或者一篇文章有很多评论,但是现在我如何在问题
show.html.erb
中提交评论
我的型号如下:
#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
<% @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 %>
def create
Question.find(params[:question_id]).comments.build
end
我的问题\u controller.rb
如下:
#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
<% @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 %>
def create
Question.find(params[:question_id]).comments.build
end
Myroot.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"}
请求
参数:
{“utf8”=>“✓",
“真实性令牌”=>“/MIczvX+UVpMnKx9K2/7cTmoiM1oDcYZO63RE10mDAlnRMJRukFq/2t24yBqfDIg8D1/midk3fu1oatw4twua==”,
“注释”=>{“内容”=>“ADFADFDAFF”},
“注释编辑器MD html代码”=>“ADFADFDAFF\r\n”,
“提交”=>“提交”,
“问题id”=>“1”}
尝试以下修复
<%= form_for [@comment.commentable,@comment], url: "#{polymorphic_path(commentable)}/comments" do |f| %>
这是一个打字错误问题吗?\u controller.rb(单数)?您的routes.rb文件中有资源问题(复数)很抱歉,你是对的,谢谢你@Shani。我又编辑了一次。如果你能把错误的整个堆栈跟踪放进去会很容易。我怎么能把整个堆栈跟踪放进去呢?对不起,我是rails的新手。你能给我举个例子或者给我看一些相关的文档吗?@Shani。非常感谢。你还看到了什么你知道路由错误吗?