我无法在Rails应用程序中添加评论系统。我遵循了rails指南see rails guide started,甚至从一个新的rails应用程序开始。但是我不确定我是否正确遵循了指南。请帮助我弄清楚我在做什么错。
CourseController
class CoursesController < ApplicationController
before_action :authenticate_user!
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :find_matieres, only: [:index, :show, :new, :edit]
# GET /courses
# GET /courses.json
def index
@courses = Course.all.order('created_at desc')
@matieres = Matiere.all
end
# GET /courses/1
# GET /courses/1.json
def show
@courses = Course.all.order('created_at desc')
end
# GET /courses/new
def new
@course = current_user.courses.build
end
# GET /courses/1/edit
def edit
end
# POST /courses
# POST /courses.json
# def create
def create
@course = current_user.courses.build(course_params)
respond_to do |format|
if @course.save
format.html { redirect_to @course, notice: 'Cours crée avec succes.' }
format.json { render :show, status: :created, location: @course }
else
format.html { render :new }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1
# PATCH/PUT /courses/1.json
def update
respond_to do |format|
if @course.update(course_params)
format.html { redirect_to @course, notice: 'Cours mise a jour avec success..' }
format.json { render :show, status: :ok, location: @course }
else
format.html { render :edit }
format.json { render json: @course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
@course.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: 'Cours supprimé avec success.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
@course = Course.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :content, :user_id, :matiere_id)
end
def find_matieres
@matieres = Matiere.all.order('created_at desc')
end
end
CommentController
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :set_course,only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
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
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @course, notice: 'modification valide.' }
format.json { render :show, status: :ok, location: @course }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# => find course_id reference
def set_course
@course = Course.find(params[:course_id])
end
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:content, :course_id)
end
end
模型课程
class Course < ApplicationRecord
#RELATIONS
belongs_to :matiere
belongs_to :user
has_many :comments
#VALIDATES
validates :title, :content, presence: true,
length: { minimum: 20 }
#validates_processing_of :image
#validate :image_size_validation
#PRIVATE
private
# def image_size_validation
# errors[:image] << "fichier trop lourd, reduire la taille a 900KB" if image.size > 0.9.megabytes
#end
end
模型注释
class Comment < ApplicationRecord
#RELATIONS
belongs_to :course
#VALIDAES
validates :comment ,presence:true,
length: { minimum: 10}
end
评论形式
<!-- Comments Form -->
<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>
评论文件夹中的_comment.html.erb
<div class="box">
<article class="media">
<!-- avatar user comment/!-->
<div class="media-left">
<figure class="image is-48x48 is-rounded">
<%= link_to edit_user_registration_path, class: "button" do %>
<img src="https://bulma.io/images/placeholders/32x32.png">
<% end %>
</figure>
</div>
<!-- comment + meta info user /!-->
<div class="media-content">
<div class="content">
<p>
<!-- ADD
Upvote/downvote comment by over user
Username
/!-->
</p><br>
<p><strong>Commenaire : </strong>
<%= comment.content %>
</p>
</div>
</div>
</article>
</div>
course / show.html.erb用于带有注释的印刷内容课程
<div class="container show_course">
<!-- meta description matiere/!-->
<small class="has-text-grey-lighter">
Sujet: <%= link_to @course.matiere.matiere, @course.matiere %>
</small><br>
<!-- course title/!-->
<h3 class="title title-discu-show">
<%= @course.title %>
</h3>
<!-- meta info user/!-->
<%= render 'shared/users/data_users' %>
<!--content course/!-->
<div class="card-content">
<%= sanitize(@course.content) %>
</div>
<!-- Add option feature/
...
modifier
bookmark
follow
add your Biblio
delete
signaler
/!--->
<div class="level">
<div class="level-right">
<% if course_author(@course) %>
<div class="buttons">
<%= link_to 'Modifier', edit_course_path(@course), class:'button'%>
</div>
<% end %>
</div>
</div>
<!-- print comments/!-->
<div id="course-comments.content">
<!-- section commentaire/!-->
<%= render @course.comments %>
</div>
<br>
<!-- form comments/!-->
<div class="course-comment-form">
<%= render 'comments/form' %>
</div>
</div>
routes.rb
Rails.application.routes.draw do
resources :accounts
resources :matieres
resources :courses do
resources :comments
end
root to:'home#index'
devise_for :users, controllers: { registrations: 'registrations' }
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'
在您的评论模型中,没有“评论”字段。您只有一个“内容”字段。您的问题在这里
validates :comment ,presence:true,
length: { minimum: 10}
在您的评论模型中。我想你的意思是>
validates :content, presence:true,
length: { minimum: 10}