注释不能保存在Rails 5中

时间:2017-01-24 23:24:34

标签: ruby-on-rails ruby ruby-on-rails-5

我正在尝试为我的博客添加评论(松散地跟随this guide)。但是,当我尝试提交评论时,一切都会通过,但评论不会保存。我检查了数据库,似乎没有创建任何东西。奇怪的是,也没有错误。

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)

2 个答案:

答案 0 :(得分:1)

如果没有服务器日志文件中的代码段,很难看出问题所在。当您使用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

答案 1 :(得分:0)

对于rails 5.2,添加到注释模型:

, optional: true

解决了这个问题