Ruby on rails 注释未保存在Rails 5中
我正在尝试向我的博客添加评论(如下)。然而,当我试图提交评论时,一切都会进行,但评论不会保存。我检查了数据库,似乎没有创建任何内容。奇怪的是,也没有错误 评论\u controller.rbRuby on rails 注释未保存在Rails 5中,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我正在尝试向我的博客添加评论(如下)。然而,当我试图提交评论时,一切都会进行,但评论不会保存。我检查了数据库,似乎没有创建任何内容。奇怪的是,也没有错误 评论\u controller.rb class CommentsController < ApplicationController def create @article = Article.find(params[:article_id]) @comment = @article.comments.create
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
<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>
class Comment < ApplicationRecord
belongs_to :article
belongs_to :user
end
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)
如果没有服务器日志文件中的一个片段,很难看出问题出在哪里。由于您使用的是Rails 5,这可能是由于新的要求属于现在默认需要关联 您可以通过添加:可选子句来更改此行为
class Comment < ApplicationRecord
belongs_to :article, optional: true
belongs_to :user, optional: true
end
class注释
您最好将注释模型转换为多态关联
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
class注释
对于rails 5.2添加到注释模型:
, optional: true
已解决此问题请发布错误跟踪并在注释模型中检查您的验证。在此行下方
@comment=@article.comments.create(comment_参数)
,添加render json:@comment.inspect并返回false
并检查/p@Sravan我得到了#
@EdmundLee没有错误跟踪。我在上面添加了日志文件的内容,但是关联不会引起错误吗?我以前把关联搞砸了,通常会出现一个明显的“找不到用户/文章”错误。现在,它只是在进行,没有说任何东西。尝试添加可选的,看看是否有帮助。在Rails 5下,关系是必需的,即:除非您指定关系是可选的,否则在article\u id和user\u id字段中都必须有某些内容。可选是Rails 5之前的默认行为。我不知道为什么它不会发出错误。谢谢。这尤其令人困惑,因为这种关系似乎有效,也就是说,我可以显示在正确文章中写评论的用户。这也解决了我的问题。荒唐的非常感谢。