【发布时间】:2016-10-07 20:07:52
【问题描述】:
我正在尝试使用“closure-tree”gem 实现评论系统,但在让 cmets 最初显示在我的项目配置文件“讨论”页面上时遇到了一些问题。
我在 cmets 模型上为 project_id 创建了一个属性,当我从项目实例创建新注释时,它根本没有设置(project_id 在 rails 控制台中返回为 nil)。我的问题是:
1) 如果我的路线和模型/控制器设置如下所示,我什至需要那个 project_id 字段吗?还有
2)如果不是(我将删除该属性),我需要更改什么以确保新创建的 cmets 以某种方式与我添加它们的项目实例相关联,以便获取
<%= render @project.comments %>
显示所有项目的cmets?提前感谢您的帮助。
我的模特:
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
我的项目控制器:
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 -->
最后是我的 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, except: [:index]
member do
get :projectadmins
get :followers
get :tasks
get :comments
end
end
resources :tasks
resources :comments
end
感谢您的帮助。
更新:
添加了 project_sidebar 部分以显示我如何传递 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>
最后是views/cmets/_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 %>
【问题讨论】:
-
你可以在你的 cmets 控制器的创建动作中添加一个 raise 吗?然后检查参数,看看是否传递了project_id
标签: ruby-on-rails ruby-on-rails-4 rails-routing comments