我有一个用于创建 Material 的表单(标题、描述和内容 - 都是基本的)。表单很好地保存了这些细节,但它没有保存 user_id,它应该是 current_user 的 user_id。我该怎么做呢?这一定很容易,但到目前为止还没有任何效果。
def create
@material = Material.new(params[:material])
if @material.save
flash[:success] = "Content Successfully Created"
redirect_to @material
else
render 'new'
end
end
最佳答案
def create
@material = Material.new(params[:material])
@material.user_id = current_user.id if current_user
if @material.save
flash[:success] = "Content Successfully Created"
redirect_to @material
else
render 'new'
end
end
关于ruby-on-rails - 如何在rails中将current_user添加到user_id到form_for?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12759683/