我得到了如下嵌套资源错误:
ActiveRecord :: RecordNotFound位于/ admin / projects / 2 / project_comments
无法找到带有'id'=
的项目的routes.rb
namespace :admin do
resources :users
resources :projects do
resources :project_users
resources :project_comments
end
end
project.rb
has_many :project_comments
project_comment.rn
belongs_to :project
project_comments_co
class Admin::ProjectCommentsController < ApplicationController
before_action :set_project_comment, only: [:show, :edit, :update, :destroy]
before_action :set_project
def your_action
# these are for debugging
puts params.inspect
puts params[:option_id]
@event = Event.find(params[:event_id])
@option = Option.find(params[:option_id])
end
def index
@project_comments = @project.project_comments.all
end
def new
@project_comment = @project.project_comments.new
end
def create
@project_comment = @project.project_comments.new(project_comment_params)
if @project_comment.save
else
end
end
def show
end
def edit
end
def update
if @project_comment.update(project_comment_params)
else
end
end
def destroy
@project_comment.destroy
end
private
def set_project
@project = Project.find(params[:id])
end
def set_project_comment
@set_project_comment = ProjectComment.find(params[:id])
end
def project_comment_params
params.require(:project_comment).permit(:project_id, :user_id, :comment)
end
端
感谢您帮助我!
答案 0 :(得分:0)
此错误表示Rails无法使用此ID找到任何项目记录。
数据库中是否有任何项目记录?
请发布堆栈跟踪。
答案 1 :(得分:0)
使用以下代码更改set_project函数:
private
def set_project
@project = Project.find(params[:project_id])
end
答案 2 :(得分:0)
当你在ProjectCommentsController
内时,params[:id]
将id
ProjectComment
而不是Project
。所以在你的方法中:
def set_project
@project = Project.find(params[:id])
end
将params[:id]
更改为params[:project_id]
,这将解决您的问题。