rails多态问题:
我现在有问题
,文章
,评论
三个模型,一个问题
对应多个评论
,一篇文章
对应多个评论
,现在怎么在问题
下提交评论
?。
我的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 "提交", class: "btn btn-primary pull-right" %>
<% end %>
我的question_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
主要问题在于这语句:
这句实质就是
<%= form_for([@question, @comment]) do |f| %>
url: "/questions/2/comments" (post请求)
也就是说你是给这个question添加评论,需要comments控制器,并定义create方法
但是你没有定义