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

标签 html ruby-on-rails ruby database save

我正在使用 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/

相关文章:

jquery - Android 浏览器因 Google Site Search 而崩溃

javascript - 当用户使用shift+enter提交时如何创建一个新的文本区域?

html - 将表格拆分为 css 列,Firefox 中的问题

javascript - 模糊事件 : get the element clicked from inside the blur event

ruby-on-rails - 如何升级到最新版本的 ruby​​ on rails 3.2.8?

javascript - 使用 jQuery 单击选择单选按钮

mysql - Rails 和数据库 : How do you do it nicely when things get messy?

ruby - 如何在 Ruby 进程中捕获信号

ruby-on-rails - 如何在 RSpec 中 stub 两个链接的 ActiveRecord 方法?

ruby - 从 Heroku 恢复本地数据库后的 ActiveRecord::PendingMigrationError