@ project.comments未显示在我的项目个人资料讨论页面中

时间:2016-06-07 19:15:09

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

我正在尝试使用'closure-tree'gem实现一个评论系统,我在查看最初显示在我的项目配置文件'讨论'页面时遇到了一些麻烦。

我在project_id的注释模型上创建了一个属性,当我从项目实例创建新注释时它根本没有设置(project_id在rails控制台中以nil的形式返回)。我的问题是:

1)如果我的路由和模型/控制器设置如下所示,我甚至需要project_id字段吗?还有

2)如果没有(我将删除该属性),我需要更改什么以确保新创建的注释以某种方式与我添加它们的项目实例相关联才能获得

<%= render @project.comments %>

显示所有项目评论?我提前感谢你的帮助。

我的模特:

class Comment < ActiveRecord::Base

  belongs_to :owner, :foreign_key=>'user_id', :class_name=>'User'
  belongs_to :project
end

class Project < ActiveRecord::Base
  belongs_to :owner, :foreign_key=>'user_id', :class_name=>'User'
  has_many :comments
end

我的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 @project.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, except: [:index]
    member do
      get :projectadmins
      get :followers
      get :tasks
      get :comments
    end
  end

  resources :tasks
  resources :comments
end

我很感激帮助。

更新:

添加了project_sidebar partial以显示我如何传递project_id:

<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>

        <% if policy(@project).comments? %>
        <li>
          <h4>
            <a href="<%= comments_project_path(@project) %>">
                Discussion
            </a>
          </h4>
        </li>
        <% end %>
<ul>

最后是观点/评论/ _form:

 <%= form_for @comment, :html => {:multipart => true} do |f| %>

  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :body %>
  <%= f.text_area :body, class: 'form-control', required: true %>

    <%= hidden_field_tag 'project_id', @project_id %>

  <br clear="all">

  <%= f.submit "Add your Comment", class: "btn btn btn-info" %>
<% end %>

2 个答案:

答案 0 :(得分:1)

您的控制器中有一个大写P,但p中有一个常规link_to

Project.find(params[:Project_id])

应该是

Project.find(params[:project_id])

我认为这就是为什么它无法找到项目的ID。

答案 1 :(得分:1)

您已使用hidden_field_tag,结果params[:project_id]正在设置,而您的代码正在尝试使用params[:comment][:project_id]

更改表单以提交该参数名称,并保持控制器不变或保持表单不变并更新控制器(您必须明确地将project_id设置为注释)。