我正在嘗試向我的博客添加評論(鬆散地跟在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)
請張貼錯誤跟蹤 –
併爲您在評論模型您的驗證。 –
'@comment = @ article.comments.create(comment_params)',添加'render json:@ comment.inspect並返回false'並檢查一下是哪一個o/p – Sravan