我正在关注这个简单博客的在线视频(here)教程 .
这些博客文章里面都有评论 .
在本教程中,使用了Devise gem并执行了迁移以将post user_id分配给帖子 .
我进行了add_user_id_to_comments的迁移,类似于在post_的user_id迁移中完成的迁移 .
class AddUserIdToComments < ActiveRecord::Migration
def change
add_column :comments, :user_id, :integer
add_index :comments, :user_id
end
end
我的模型包含以下内容:
Comment model
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
User model
class User < ActiveRecord::Base
has_many :posts
has_many :comments
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我可以为执行以下操作的用户分配注释 .
user = User.first
comment = Comment.first
comment.user = user
这适用于控制台,但我遇到的问题是comments_controller . 我试图模仿使用后置控制器完成的步骤 .
这是来自 Post controller (工作正常)
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :post_auth, only: [:edit, :update, :destroy]
def index
@posts = Post.all.order('created_at DESC')
end
def new
#@post = Post.new
@post = current_user.posts.build
end
def create
#@post = Post.new(post_params)
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
end
def edit
end
def update
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
# Checks if current user authored pin
def post_auth
if current_user != @post.user
redirect_to(root_path)
end
end
end
Comments controller 我遇到了困难
class CommentsController < ApplicationController
before_action :find_comment, only: [:create, :destroy]
before_action :comment_auth, only: [:destroy]
def create
#@post = Post.find(params[:post_id])
#@comment = @post.comments.create(params[:comment].permit(:name, :body))
#redirect_to post_path(@post)
#new format
@post = Post.find(params[:post_id])
#post = current_user.posts.build(post_params)
@comment = current_user.comments.build(comment_params)
redirect_to post_path(@post)
end
def destroy
@comment = @post.comments.find(params[:id]).destroy
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:name, :body)
end
def find_comment
@post = Post.find(params[:post_id])
end
def comment_auth
if current_user != @post.user
redirect_to(root_path)
end
end
end
当我尝试创建新评论时;没有任何反应......它重定向到帖子页面 . 但是没有返回任何错误 .
您的想法如何解决这个问题?任何帮助将不胜感激 .
编辑w /解决方案
我遇到了' name 和 body 值未传递到数据库的问题 . 经过多次尝试,我终于能够使用以下代码传递 name 和 body 值:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, :body))
@comment.user_id=current_user.id if current_user
@comment.save
if @comment.save
redirect_to post_path(@post)
else
render 'new'
end
谢谢大家的帮助
3 回答
您在评论中调用了
build
,然后没有保存它们 . 您需要save
或使用create
.使用
build
方法时,将返回一个对象,但尚未将其保存到数据库中 . 你有两个选择:要么,
create
方法,自动保存(注意我没有't tested this, feedback welcome if I'告诉谎言!):在Rails docs(强调我的)中看到两者之间的差异:
在创建操作中,您没有保存
@comment
,而只是构建它 . 你应该拨打@comment.save
或current_user.comments.create(comment_params)
. 为了说明我的意思:要么:
或者,这将有效 .