2017-01-24 43 views
0

我正在嘗試向我的博客添加評論(鬆散地跟在this guide之後)。但是,當我嘗試提交評論時,所有內容都會經過,但評論並未保存。我檢查了數據庫,似乎沒有創建任何東西。奇怪的是,也沒有錯誤。評論未保存在Rails 5中

comments_controller.rb

class CommentsController < ApplicationController 

    def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comment_params) 
    redirect_to article_path(@article) 
    end 

    private 
    def comment_params 
    params.require(:comment).permit(:body, :users_id) 
    end 
end 

show.html.erb

<div class="comment"> 
     <h2>Comments</h2> 
      <% @article.comments.each do |comment| %> 
      <p> 
       <strong>Commenter:</strong> 
       <%= comment.user_id %> 
      </p> 

      <p> 
       <strong>Comment:</strong> 
       <%= comment.body %> 
      </p> 
     <% end %> 
    </div> 
    <div class="comment-form"> 
     <h2>Add a comment:</h2> 
     <%= form_for([@article, @article.comments.build]) do |f| %> 
      <% if @article.errors.any? %> 
       <div> 
        <h2> 
         <%= pluralize(@article.errors.count, "error") %> prohibited 
         this article from being saved: 
        </h2> 
        <ul> 
         <% @article.errors.full_messages.each do |msg| %> 
          <li><%= msg %></li> 
         <% end %> 
        </ul> 
       </div> 
      <% end %> 
      <p> 
       <%= f.label :body %><br> 
       <%= f.text_area :body %> 
      </p> 
       <%= f.hidden_field :users_id, :value => session[:user_id] %> 
      <p> 
       <%= f.submit %> 
      </p> 
     <% end %> 
    </div> 

comment.rb

class Comment < ApplicationRecord 
    belongs_to :article 
    belongs_to :user 
end 

schema.rb

ActiveRecord::Schema.define(version: 20170122050809) do 

    create_table "articles", force: :cascade do |t| 
    t.string "title" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.text  "body" 
    t.integer "user_id" 
    t.index ["user_id"], name: "index_articles_on_user_id" 
    end 

    create_table "ckeditor_assets", force: :cascade do |t| 
    t.string "data_file_name",    null: false 
    t.string "data_content_type" 
    t.integer "data_file_size" 
    t.string "data_fingerprint" 
    t.integer "assetable_id" 
    t.string "assetable_type", limit: 30 
    t.string "type",    limit: 30 
    t.integer "width" 
    t.integer "height" 
    t.datetime "created_at",     null: false 
    t.datetime "updated_at",     null: false 
    t.index ["assetable_type", "assetable_id"], name: "idx_ckeditor_assetable" 
    t.index ["assetable_type", "type", "assetable_id"], name: "idx_ckeditor_assetable_type" 
    end 

    create_table "comments", force: :cascade do |t| 
    t.text  "body" 
    t.integer "article_id" 
    t.integer "users_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.index ["article_id"], name: "index_comments_on_article_id" 
    t.index ["users_id"], name: "index_comments_on_users_id" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "name" 
    t.string "password_digest" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.boolean "is_admin" 
    end 

end 

編輯:段從日誌。正如你可以看到有沒有明顯的錯誤

Started POST "/articles/1/comments" for 127.0.0.1 at 2017-01-24 18:12:49 -0500 
    [1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1m[34mSELECT "schema_migrations".* FROM "schema_migrations"[0m 
Processing by CommentsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"YJDPGei0U1SMPMzAuZggad4LxUm7D24VDOHbm41fkHhh2s29tD1ufoLeoYoNvyaSejjBcmJyD/vL+T7no8KWsw==", "comment"=>{"body"=>"hello", "users_id"=>"1"}, "commit"=>"Create Comment", "article_id"=>"1"} 
    [1m[36mUser Load (0.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 1], ["LIMIT", 1]] 
    [1m[36mArticle Load (0.1ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?[0m [["id", 1], ["LIMIT", 1]] 
    [1m[35m (0.0ms)[0m [1m[36mbegin transaction[0m 
    [1m[35m (0.1ms)[0m [1m[36mcommit transaction[0m 
Redirected to http://localhost:3000/articles/1 
Completed 302 Found in 69ms (ActiveRecord: 1.5ms) 

另一個日誌片斷:

Started POST "/articles/2/comments" for 127.0.0.1 at 2017-01-25 02:58:20 -0500 
Processing by CommentsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"hn5M1vHtv1fdtCDxz8RK3QIHtKNCxLOxdGywlR+fKtWHNE5yrWSCfdNWTbt740wmpjSwmJu50l+zdFXpMQIsHg==", "comment"=>{"body"=>"this is for stackoverflow", "users_id"=>"1"}, "commit"=>"Create Comment", "article_id"=>"2"} 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 
    Article Load (0.1ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 
    (0.1ms) begin transaction 
    (0.1ms) commit transaction 
Completed 200 OK in 6ms (Views: 0.1ms | ActiveRecord: 0.3ms) 
+0

請張貼錯誤跟蹤 –

+0

併爲您在評論模型您的驗證。 –

+0

'@comment = @ article.comments.create(comment_params)',添加'render json:@ comment.inspect並返回false'並檢查一下是哪一個o/p – Sravan

回答

0

很難看到的問題是什麼,而不從服務器日誌文件中的一個片段。當您使用Rails 5時,可能由默認情況下需要現在需要belongs_to關聯的新要求導致。

您可以通過添加更改此行爲:可選條款

class Comment < ApplicationRecord 
    belongs_to :article, optional: true 
    belongs_to :user, optional: true 
end 

您也可能會更好轉動註釋模型到多態關聯。

class Comment < ApplicationRecord 
    belongs_to :commentable, polymorphic: true 
end 

class Article < ApplicationRecord 
    has_many :comments, as: :commentable 
end 

class User < ApplicationRecord 
    has_many :comments, as: :commentable 
end 
+0

雖然關聯不會引發錯誤嗎?我之前搞錯了關聯,通常會引發一個明顯的「無法找到用戶/文章」錯誤。現在它只是沒有說任何東西 –

+0

嘗試添加可選,看看是否有幫助。 – ardochhigh

+0

好的,那有效。但爲什麼_? –