Я создаю приложение для форума в 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 %>
и модели сообщений и комментариев.
post:
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
Спасибо за помощь.