i'm trying implement commenting system using 'closure-tree' gem , i'm getting following error when trying click on link comments page of project profile (the comments_project_path):
nomethoderror @ /projects/1/comments undefined method `id' nil:nilclass
the error refers line in project_sidebar partial contains several of links different pages project instance (the comments page seen above several other routed project pages).
my views/projects/_project_sidebar.html.erb
the link_to below highlighted in 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 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>
my projectscontroller:
def comments @title = "project comments" @project = project.find(params[:id]) @comments = @project.comments render 'show_project_discussion' end
my commentscontroller:
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 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 partial:
<div class="container middle"> <!-- sidebar need refactor 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 button below start 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 -->
lastly routes.rb:
rails.application.routes.draw devise_for :users resources :users collection patch :update, as: :update end member :following, as: :users_following :profile, as: :profile end end resource :profile, only: [:show, :update] resources :projects 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 :autocomplete_user_email, :on => :collection end resources :projects resources :comments member :projectadmins :followers :tasks :comments end end resources :tasks resources :comments end
i appreciate help.
i think it's order of operations in routes.rb
.
this:
resources :comments
generates regex matchers 7 actions (index, show, new, edit, update, destroy, create)
regex index
matches member route trying use:
/projects/1/comments
since routes returns first matched value, request processed comments#index
@project
not set. error when call id
on nil
.
to fix, suggest limiting matchers generated resources
:
resources :projects resources :comments, except: [:index] end