2016-09-04 169 views
1

我在rails上使用ruby來創建論壇網站,使用我在YouTube上找到的教程。到現在爲止,我已經完成了80%的任務。我已經重新觀看了視頻10次以確保沒有語法錯誤或任何其他意外。基本上,人們對帖子的評論並沒有保存到數據庫中,因此他們沒有在我呈現的html視圖中顯示。我知道他們沒有保存,因爲我在終端中查看了評論的數據庫,並將其作爲0計數返回。這是我在不同的文件中的代碼...爲什麼我的論壇中的評論沒有保存到數據庫?

的routes.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 

comments_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) 
+0

您可以添加評論模型文件嗎?它有什麼驗證? – ArtOfCode

+0

你實際上在這裏有兩個問題 - 其中一個,你的評論沒有保存,兩個,你的'評論#新'視圖沒有找到。爲了解決第二個問題,你可以嘗試將'render'new''改爲'render:new'嗎? – ArtOfCode

+0

我只是沒有提出'新'的觀點,但 – koz

回答

1

Comment#create調用失敗,因爲Comment模式需要Userbelongs_to協會皆預設存在驗證),而你並沒有設置一個。

要解決這個問題,請設置一個用戶。

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

(如果你使用的設計;否則,發現你的用戶的另一種方式)

然後繼續你的代碼,你(@comment.save)。

+0

美麗。像魅力一樣工作。非常感謝你。我是這個所有人的新手:) – koz

+0

很高興它工作:) –

+0

@koz點擊答案旁邊的綠色複選標記可以讓你指出答案的工作。 (如果您認爲答案有用,您也可以通過點擊向上箭頭來點贊。) – ArtOfCode