简体   繁体   中英

Rails4 polymorphic how to use in html page?

I used Rails-4.2.8.

Now I had question article comments models, on question has many comments, or one article has many comments, but now how could I submit the comment in question show.html.erb ?

My models as follows:

#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

My question show.html.erb as follows:

<% @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 %>

My questions_controller.rb as follows:

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

My roots.rb as follows

resources :questions do
    resources :comments
  end

But I got this error, could someone give me some suggests that where I had the wrong codes? Thanks so much!

Routing Error
uninitialized constant CommentsController

在此处输入图片说明

And also:

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"}

尝试以下修复

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM