gpt4 book ai didi

html - 为什么我在论坛中的评论没有保存到数据库中?

转载 作者:搜寻专家 更新时间:2023-10-30 23:37:54 26 4
gpt4 key购买 nike

我正在使用 ruby​​ on rails 创建一个论坛网站,使用我在 youtube 上找到的教程。到目前为止,我已经毫无问题地完成了 80%。我已经将视频重新观看了 10 多次,以确保没有语法错误或任何其他错误。基本上,人们对帖子的评论不会保存到数据库中,因此它们不会显示在我展示它们的 html View 中。我知道他们没有保存,因为我在终端中检查了评论数据库,结果显示为 0。这是我在不同文件中的代码...

路线.RB

Rails.application.routes.draw do
devise_for :users

resources :posts do
resources :comments
end

root 'posts#index'
end

create_comments 的迁移文件

class CreateComments < ActiveRecord::Migration[5.0]
def change
create_table :comments do |t|
t.text :comment
t.references :post, foreign_key: true
t.references :user, foreign_key: true

t.timestamps
end
end
end

评论 Controller .rb

class CommentsController < ApplicationController

def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:comment))

if @comment.save
redirect_to post_path(@post)
else
render 'new'
end
end
end

_form.html.haml

= simple_form_for([@post, @post.comments.build]) do |f|
= f.input :comment
= f.submit

模型文件 comment.rb

class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end

提交表单时记录

Started POST "/posts/2/comments" for ::1 at 2016-09-04 23:00:46 +1000
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/Un6QNWL4BIUbjH5VYMhLRatTq2hokcKnZ3Jb4WzTlvhuZ5AN3gFkA5VHN2E6zsm0iDIx/sKarEfID7Nx4WwwQ==", "comment"=>{"comment"=>"1"}, "commit"=>"Create Comment", "post_id"=>"2"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 26ms (ActiveRecord: 0.5ms)



ActionView::MissingTemplate (Missing template comments/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/Users/koz/Desktop/forum/app/views"
* "/Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/views"
):

app/controllers/comments_controller.rb:11:in `create'
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb within rescues/layout
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (14.6ms)
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.4ms)
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.9ms)
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb within rescues/layout (297.8ms)

最佳答案

您的 Comment#create 调用失败,因为 Comment 模型需要一个 User (belongs_to 关联随默认情况下存在验证),而您没有设置。

要解决它,请设置一个用户。

@comment = @post.comments.create(params[:comment].permit(:comment))
@comment.user = current_user

(如果您使用的是 Devise;否则,请以其他方式找到您的用户)

然后继续您的代码(@comment.save)。

关于html - 为什么我在论坛中的评论没有保存到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com