nil的未定义方法`id':由项目侧栏链接引起的NilClass

时间:2016-06-07 02:27:55

标签: ruby-on-rails ruby-on-rails-4 rails-routing nomethoderror

我正在尝试使用'closure-tree'gem实现注释系统,当我尝试点击项目配置文件的Comments页面的链接(comments_project_path)时,我收到以下错误:

NoMethodError at /projects/1/comments
undefined method `id' for nil:NilClass

错误是指我的project_sidebar部分中的一行,其中包含指向项目实例的不同页面的几个链接(上面显示的注释页面以及其他几个路由项目页面)。

我的观点/项目/ _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>

我的ProjectsController:

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

观看/项目/ 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 -->

最后我的路线.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

我很感激帮助。

1 个答案:

答案 0 :(得分:1)

我认为这与您routes.rb中的操作顺序有关。

此:

resources :comments

为所有7个动作生成正则表达式匹配器(索引,显示,新建,编辑,更新,销毁,创建)

index的正则表达式与您尝试使用的成员路由匹配:

/projects/1/comments

由于路由返回第一个匹配的值,因此请求由Comments#index处理,其中未设置@project。因此,当您在id上致电nil时,您会收到错误。

要修复,我建议限制resources生成的匹配器:

resources :projects do
  resources :comments, except: [:index]
end