2017-04-19 27 views
0

我得到這個嵌套資源錯誤象下面這樣:ruby​​-無法與 'ID' 找項目=

的ActiveRecord :: RecordNotFound在/管理/項目/ 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

在你的set_project方法中,params [:id]應該是params [:project_id]我猜 – Deep

+0

謝謝!我現在解決了 – JiaPing

回答

0

如你是ProjectCommentsControllerparams[:id]內會給你的ProjectCommentid而不是Project。因此,在你的方法:

def set_project 
    @project = Project.find(params[:id]) 
end 

更改params[:id]params[:project_id]這將解決您的問題。

0

這個錯誤說Rails無法找到任何項目與此ID的記錄。

數據庫中是否有任何項目記錄?

請發佈堆棧跟蹤。

+0

我把params [:id]改成params [:project_id],錯誤就解決了。感謝你們對我的幫助! – JiaPing

0

與更改set_project功能下面的代碼:

private 
def set_project 
    @project = Project.find(params[:project_id]) 
end