注释未保存到数据库-无法自动加载常量CommentController

时间:2018-09-30 01:36:15

标签: ruby-on-rails ruby

我正在RoR中构建一个论坛应用程序,目前无法在数据库中保存评论。

按下“创建评论”按钮时,终端上会出现以下错误:

  

LoadError(无法自动加载常量CommentController,应该是   /..//app/controllers/comment_controller.rb进行定义)

这是我的路线:

devise_for :users
root                              to: 'post#index'

#Posts
get    '/posts/new',             to: 'post#new' 
get    '/post/:id',              to: 'post#show',      as: 'show'
get    '/post/:id/edit',         to: 'post#edit',      as: 'edit'
post   '/post',                  to: 'post#create'
put    '/post/:id',              to: 'post#update',    as: 'update'
delete '/post/:id',              to: 'post#destroy',   as: 'delete' 

#Comments
post  '/post/:post_id',  to: 'comment#create', as: 'new_comments'

comment_controller.rb:

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user! 

  def index
    @comments = Comment.all
  end

  def new
    @comment = Comment.new
  end

  def create 
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(comment_params)
    @comment.user = current_user
    @comment.assign_attributes(comment_params)


      if @comment.save
        format.html { redirect_to @link, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end


  end

  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to :back, notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # 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(:post_id, :content, :title, :user_id)
    end


end

html格式:

<div id="comments-form">
 <%= form_for @comment, url: new_comments_path(@post), method: :post, remote: true do |f| %>
    <%= f.label :title %><br>
    <%= f.text_field :title, placeholder: "Type the comment title here" %><br>
    <%= f.label :content %><br>
    <%= f.text_area :content, placeholder: "Type the comment content here" %><br>
  <%= f.submit %>
<% end %>

以及帖子和评论模型。

帖子:

class Post < ApplicationRecord
    belongs_to :user
    has_many :comments
    validates :title, :content, presence: true
end

评论:

class Comment < ApplicationRecord
    belongs_to :user
    belongs_to :post
    has_many :reviews
end

谢谢您的帮助。

2 个答案:

答案 0 :(得分:0)

刚发现错误,我错误地定义了控制器类。

它应该读类CommentController < ApplicationController而不是类

CommentsController < ApplicationController

答案 1 :(得分:0)

总是使用类似的资源

resources :comments

因此该url将自动生成,如果您想在post下进行嵌套的url,则可以这样做

resources :posts do
   resources :comments
end

为进行更正,您的控制器名称正确,如“ Comments”作为rails约定,您需要在此处指定如下:

#Comments
post  '/post/:post_id',  to: **'comments#create**', as: 'new_comments'