nil:NilClass 的未定义方法“id”由项目边栏链接引起
undefined method `id' for nil:NilClass caused by project sidebar links
我正在尝试使用 'closure-tree' gem 实现评论系统,当我尝试点击 link 进入评论页面时出现以下错误项目简介(comments_project_path):
NoMethodError at /projects/1/comments
undefined method `id' for nil:NilClass
错误指的是我的 project_sidebar 部分中的一行,其中包含多个 link 项目实例的不同页面(上面看到的评论页面以及其他几个路由项目页)。
我的views/projects/_project_sidebar.html.erb
THE LINK_TO JUST BELOW IS HIGHLIGHTED IN THE ERROR
<%= link_to "+ Submit Task", new_task_path(:project_id=> @project.id), :class => "btn btn-info col-md-12" %>
<br/>
<ul class="sidebar-menu">
<div class="sidebar-header">
<h4 class="head">Explore this Project</h4>
</div>
<li>
<h4>
<a href="<%= project_path(@project) %>">
Overview
</a>
</h4>
</li>
<li>
<h4>
<a href="<%= tasks_project_path(@project) %>">
Tasks
</a>
</h4>
</li>
<li>
<h4>
<a href="<%= comments_project_path(@project) %>">
Discussion
</a>
</h4>
</li>
</ul>
我的项目控制器:
def comments
@title = "Project Comments"
@project = Project.find(params[:id])
@comments = @project.comments
render 'show_project_discussion'
end
我的评论控制器:
class CommentsController < ApplicationController
before_filter :authenticate_user!, only: [:create, :new, :edit, :update, :delete]
def index
@comments = Comment.all
end
def new
@project_id = params[:project_id]
@comment = Comment.new
end
def create
@project = Project.find(params[:Project_id])
@comment = current_user.own_comments.build(comment_params)
if @comment.save
flash[:success] = 'Your comment was posted!'
redirect_to root_url
else
render 'new'
end
end
private
def comment_params
params.require(:comment).permit(:body, :project_id, :user_id)
end
end
Views/Projects/Show_Project_Discussion部分:
<div class="container middle">
<!-- SideBar NEED TO REFACTOR TO A USER LAYOUT FILE -->
<div class="sidebar col-md-3">
<div class="sidebar-content">
<div class="sidebar-pad">
<%= render 'project_sidebar' %>
</div>
</div>
</div>
<div class="main-content col-md-9">
<div class="main-breadcrumb">
</div>
<div class="section_header">
<h3>Discussion</h3>
<p>Click the button below to start a new thread:</p>
<p>
<%= link_to "+ Add New Comment", new_project_comment_path(:project_id=> @project.id), :class => "btn btn-info col-md-4" %>
</p>
</div>
<%= render @comments %>
</div>
</div><!-- end Container -->
最后我的 Routes.RB:
Rails.application.routes.draw do
devise_for :users
resources :users do
collection do
patch :update, as: :update
end
member do
get :following, as: :users_following
get :profile, as: :profile
end
end
resource :profile, only: [:show, :update]
resources :projects do
match '/settings'=>'projects#settings', :via=>:get, :as=>:settings
match '/invites'=>'projects#invites', :via=>:get, :as=>:invites
match '/invite_admin'=>'projects#invite_admin', :via=>:patch, :as=>:invite_admin
get :autocomplete_user_email, :on => :collection
end
resources :projects do
resources :comments
member do
get :projectadmins
get :followers
get :tasks
get :comments
end
end
resources :tasks
resources :comments
end
感谢您的帮助。
我认为这与您 routes.rb
中的操作顺序有关。
这个:
resources :comments
为所有 7 个操作(索引、显示、新建、编辑、更新、销毁、创建)生成正则表达式匹配器
index
的正则表达式匹配您尝试使用的成员路由:
/projects/1/comments
由于路由 returns 第一个匹配值,请求由 Comments#index
处理,其中 @project
未设置。因此,当您在 nil
.
上调用 id
时会出现错误
要修复,我建议限制 resources
:
生成的匹配器
resources :projects do
resources :comments, except: [:index]
end
我正在尝试使用 'closure-tree' gem 实现评论系统,当我尝试点击 link 进入评论页面时出现以下错误项目简介(comments_project_path):
NoMethodError at /projects/1/comments
undefined method `id' for nil:NilClass
错误指的是我的 project_sidebar 部分中的一行,其中包含多个 link 项目实例的不同页面(上面看到的评论页面以及其他几个路由项目页)。
我的views/projects/_project_sidebar.html.erb
THE LINK_TO JUST BELOW IS HIGHLIGHTED IN THE ERROR
<%= link_to "+ Submit Task", new_task_path(:project_id=> @project.id), :class => "btn btn-info col-md-12" %>
<br/>
<ul class="sidebar-menu">
<div class="sidebar-header">
<h4 class="head">Explore this Project</h4>
</div>
<li>
<h4>
<a href="<%= project_path(@project) %>">
Overview
</a>
</h4>
</li>
<li>
<h4>
<a href="<%= tasks_project_path(@project) %>">
Tasks
</a>
</h4>
</li>
<li>
<h4>
<a href="<%= comments_project_path(@project) %>">
Discussion
</a>
</h4>
</li>
</ul>
我的项目控制器:
def comments
@title = "Project Comments"
@project = Project.find(params[:id])
@comments = @project.comments
render 'show_project_discussion'
end
我的评论控制器:
class CommentsController < ApplicationController
before_filter :authenticate_user!, only: [:create, :new, :edit, :update, :delete]
def index
@comments = Comment.all
end
def new
@project_id = params[:project_id]
@comment = Comment.new
end
def create
@project = Project.find(params[:Project_id])
@comment = current_user.own_comments.build(comment_params)
if @comment.save
flash[:success] = 'Your comment was posted!'
redirect_to root_url
else
render 'new'
end
end
private
def comment_params
params.require(:comment).permit(:body, :project_id, :user_id)
end
end
Views/Projects/Show_Project_Discussion部分:
<div class="container middle">
<!-- SideBar NEED TO REFACTOR TO A USER LAYOUT FILE -->
<div class="sidebar col-md-3">
<div class="sidebar-content">
<div class="sidebar-pad">
<%= render 'project_sidebar' %>
</div>
</div>
</div>
<div class="main-content col-md-9">
<div class="main-breadcrumb">
</div>
<div class="section_header">
<h3>Discussion</h3>
<p>Click the button below to start a new thread:</p>
<p>
<%= link_to "+ Add New Comment", new_project_comment_path(:project_id=> @project.id), :class => "btn btn-info col-md-4" %>
</p>
</div>
<%= render @comments %>
</div>
</div><!-- end Container -->
最后我的 Routes.RB:
Rails.application.routes.draw do
devise_for :users
resources :users do
collection do
patch :update, as: :update
end
member do
get :following, as: :users_following
get :profile, as: :profile
end
end
resource :profile, only: [:show, :update]
resources :projects do
match '/settings'=>'projects#settings', :via=>:get, :as=>:settings
match '/invites'=>'projects#invites', :via=>:get, :as=>:invites
match '/invite_admin'=>'projects#invite_admin', :via=>:patch, :as=>:invite_admin
get :autocomplete_user_email, :on => :collection
end
resources :projects do
resources :comments
member do
get :projectadmins
get :followers
get :tasks
get :comments
end
end
resources :tasks
resources :comments
end
感谢您的帮助。
我认为这与您 routes.rb
中的操作顺序有关。
这个:
resources :comments
为所有 7 个操作(索引、显示、新建、编辑、更新、销毁、创建)生成正则表达式匹配器
index
的正则表达式匹配您尝试使用的成员路由:
/projects/1/comments
由于路由 returns 第一个匹配值,请求由 Comments#index
处理,其中 @project
未设置。因此,当您在 nil
.
id
时会出现错误
要修复,我建议限制 resources
:
resources :projects do
resources :comments, except: [:index]
end