0
我目前正在研究我的项目,但遇到一些挑战。我有一个项目模型,有评论对象嵌套到它我可以创建一个新的评论,但当我尝试编辑与项目对象关联的评论我在浏览器中得到这个奇怪的错误,显示我想编辑评论属于一个不存在的项目。其实当我检查网址时,项目的ID并不存在。例如,如果我想编辑属于ID为1的项目的注释,该错误消息指示我想编辑属于具有ID 2的项目的注释。我尝试了所有我知道但可以'没有找到问题的根源。 这是我的意见控制器看起来像嵌套在导轨中的路由错误
[8:04] 类CommentsController <的ApplicationController before_action:find_project
def index
@projects = Project.all
if params[:project_id]
@comments = @project.comments.all
redirect_to @comments
end
end
def new
if params[:project_id]
@project = Project.find(params[:project_id])
@comment = @project.comments.build
end
end
def create
if params[:project_id]
@project = Project.find(params[:project_id])
@comment = @project.comments.create(comment_params)
if @comment.save
redirect_to @comment
else
render "new"
end
end
end
def show
if params[:project_id]
@project = Project.find_by(:id => params[:id])
@comment = @project.comments.find_by(:id => params[:id])
end
end
def edit
if params[:project_id]
@comment = @project.comments.find_by(:id => params[:id])
end
end
def update
if params[:project_id]
@comment = @project.comments.find_by(:id => params[:id])
@comment.update(comment_params)
redirect_to @comment
render "edit", notice: "You cannot update the comment"
end
end
def destroy
@comment = @project.comments.find_by(:id => params[:id])
@comment.destroy
flash[:notice] = "Comment has been successfully deleted"
redirect_to @project
end
private
def find_project
@project = Project.find_by(id: params[:project_id])
end
def comment_params
params.require(:comment).permit(:content, :project_id)
end
end
Tyesh,我试过了你的建议,但它仍然没有工作 –