簡體   English   中英

Rails4多態如何在HTML頁面中使用?

[英]Rails4 polymorphic how to use in html page?

我使用了Rails-4.2.8。

現在我有一個question article comments模型,一個問題有很多評論,或者一個文章有很多評論,但是現在我如何在question show.html.erb提交comment

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

嘗試以下修復

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM