Ich versuche, ein Kommentarsystem mit dem' closure-tree'-Juwel zu implementieren, und ich bekomme den folgenden Fehler, wenn ich versuche, auf einen Link zu klicken die Kommentare Seite eines Projektprofil (das comments_project_path):undefined Methode `id 'für nil: NilClass verursacht durch Projekt sidebar Links
NoMethodError at /projects/1/comments
undefined method `id' for nil:NilClass
der Fehler auf eine Linie in meinem project_sidebar bezieht sich teilweise die für das Projekt beispielsweise auf verschiedenen Seiten einige der Links enthält (die Kommentare Seite oben als auch gesehen wie mehrere andere geroutete Projektseiten).
Meine Ansichten/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>
Mein ProjectsController:
def comments
@title = "Project Comments"
@project = Project.find(params[:id])
@comments = @project.comments
render 'show_project_discussion'
end
Mein 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 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
Ansichten/Projekte/Show_Project_Discussion Teil:
<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 -->
schließlich Mein 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
Ich schätze die Hilfe.
Yep ... das war es! Danke Petr. – BB500