部署我的应用程序时,我的评论有问题。本地一切正常。来自 heroku 的日志说:
ActiveModel::MissingAttributeError (can't write unknown attribute `user_id`):
2018-01-02T15:52:43.461794+00:00 app[web.1]: [79cd7190-e2d9-4dd0-bf71-
709552e5c6e5] app/controllers/comments_controller.rb:15:in `create'
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么错误。也许一些数据库的东西?
我的评论控制器
class CommentsController < ApplicationController
def create
@post =Post.find(params[:post_id])
@comment =@post.comments.create(params[:comment].permit(:name, :body).merge(user: current_user))
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
if current_user.id == @comment.user_id
@comment.destroy
end
redirect_to post_path(@post)
end
end
Run Code Online (Sandbox Code Playgroud)
我的模特
class User < ApplicationRecord
has_many :posts
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Post < ApplicationRecord
belongs_to :user, optional: true
has_many :comments, dependent: :destroy
validates :title, presence: true, …Run Code Online (Sandbox Code Playgroud)