我正在尝试将我使用 Devise 创建的用户与帖子相关联。但是每当我尝试创建以用户身份登录的帖子时,我都会收到标题中提到的错误。非常感谢:)
class PostsController < ApplicationController
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new(params.require(:post).permit(:task))
@post.user = current_user
if @post.save
redirect_to @post, alert:"Post created successfully."
else
redirect_to new_post_path, alert: "Error creating post."
end
end
def show
@post = Post.find(params[:id])
end
end
用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
后模型
class Post < ActiveRecord::Base
validates_presence_of :task
belongs_to :user
end
最佳答案
对于添加用户 id 以固定以下对我有用
rails g migration add_user_to_pin user_id:integer
关于ruby-on-rails - 未知属性 `user_id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24718311/