我无法在Rails应用程序中添加评论系统。我遵循Rails指南see rails guide started,甚至从一个新的Rails应用开始。但是我不确定我是否正确遵循了指南。请帮我弄清楚我在做什么错。
CommentsController
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@course = Course.find(params[:course_id])
@comment = @course.comments.build(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @course, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @course }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
def comment_params
params.require(:comment).permit(:content, :course_id)
end
end
模型课程
class Course < ApplicationRecord
has_many :comments
end
模型评论
class Comment < ApplicationRecord
belongs_to :course
validates :comment, presence: true, length: { minimum: 10}
end
评论表
<%= simple_form_for([@course, @course.comments.build], remote: true) do |f| %>
<div class="card my-4">
<h5 class="card-header">Interaction</h5>
<div class="card-body">
<div class="form-group">
<%= f.input :content, required: true, input_html: { class: 'textarea' }, wrapper: false, label_html: { class: 'label' }, placeholder:'Ajouter une question ou un commentaire...' %>
</div>
<button type="submit" class="btn btn-primary"><i class="fa fa-location-arrow" aria-hidden="true"></i> Ajouter</button>
</div>
</div>
<% end %>
routes.rb
Rails.application.routes.draw do
resources :courses do
resources :comments
end
root to:'home#index'
end
schema.rb
create_table "comments", force: :cascade do |t|
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "course_id"
t.bigint "user_id"
end
create_table "courses", force: :cascade do |t|
t.string "title"
t.text "content"
t.string "image_course"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.bigint "matiere_id"
t.bigint "user_id"
t.index ["matiere_id"], name: "index_courses_on_matiere_id"
t.index ["user_id"], name: "index_courses_on_user_id"
end
控制台消息 注意:**** ROLLBACK **在线28 **
Started POST "/courses/4/comments" for ::1 at 2019-11-30 11:30:11 +0000
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"content"=>"100 ans, c'est très long. Je vous présente un pays qui n'a mis que deux ans à devenir un pays du tiers monde issu d'un pays riche et développé. C'est toujours un pays du tiers monde. Fortement et fièrement."}, "course_id"=>"4"}
Course Load (1.3ms) SELECT "courses".* FROM "courses" WHERE "courses"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:27
(0.8ms) BEGIN
↳ app/controllers/comments_controller.rb:28
(0.6ms) ROLLBACK
↳ app/controllers/comments_controller.rb:28
Completed 500 Internal Server Error in 33ms (ActiveRecord: 3.2ms)
NoMethodError - undefined method `comment' for #<Comment:0x0000555ccc6a8528>
Did you mean? content:
app/controllers/comments_controller.rb:28:in `create'
source=rack-timeout id=ca133680-ce55-4b88-99ce-16fd3d1f01a0 timeout=15000ms service=130ms state=completed
但是我必须在其中定义评论
在终端错误消息上说:
NoMethodError - undefined method `comment' for #<Comment:0x00007f04245dc0a8>
Did you mean? content:
app/controllers/comments_controller.rb:33:in `block in create'
app/controllers/comments_controller.rb:32:in `create'
答案 0 :(得分:2)
在Comment模型中,没有“ comment”字段。您只有一个“内容”字段。 您的问题是
validates :comment ,presence:true,
length: { minimum: 10}
在您的评论模型中。我认为您的意思是
validates :content, presence:true,
length: { minimum: 10}