【发布时间】:2016-02-08 16:51:50
【问题描述】:
在本地我可以创建评论就好了,但是在 heroku 上我得到了这个错误
ActiveRecord::UnknownAttributeError (unknown attribute 'user_id' for Comment.):
app[web.1]: app/controllers/blog/comments_controller.rb:9:in `create'
Parameters: {"utf8"=>"✓", "authenticity_token"=>"g83t8MpETbolCJq0vYku4SbnaZAro5ggXUI1PGVFJ18xneGcGCWQPjXHTfgBKBIP2/5WqHjfxf2d12+Pyq4PNA==", "comment"=>{"name"=>"test", "email"=>"tester@kostis.com", "body"=>"teste", "user_id"=>"1", "post_id"=>"1"}, "commit"=>"Create comment"}
这是我的控制器
class Blog::CommentsController < Blog::BaseController
def new
@comment = Comment.new
end
def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:success] = "Comment successfully created!"
redirect_to blog_post_url(@comment.post)
else
flash[:warning] = "There was an error with your comment. Please scroll down to see the errors."
@post = @comment.post
render 'blog/posts/show'
end
end
def edit
@comment = Comment.find_by(id: params[:id])
end
def update
@comment = Comment.find_by(id: params[:id])
if @comment.update(comment_params)
flash[:success] = "Successfully updated..."
redirect_to blog_post_path(@comment.post)
else
render :edit
end
end
private
def comment_params
params.require(:comment).permit(:body,:name,:email,:user_id,:post_id)
end
end
更新:
我的本地迁移文件是这个
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :name
t.string :email
t.text :body
t.boolean :spam, default: false
t.references :post, index: true, foreign_key: true
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
但是通过在 heroku 上运行 rails c 并检查实际的“评论”结构,没有 user_id,这怎么可能?
【问题讨论】:
-
您是否忘记在部署时迁移数据库?
-
迁移正常。一切都已迁移。
标签: ruby-on-rails ruby ruby-on-rails-4 heroku ruby-on-rails-3.2