建立一个嵌套的评论

  1. 建立数据库结构和嵌套视图(使用Stimulus取元素和绑event)
  2. 可以删除评论,可以对嵌套视图的层数进行控制.
  3. 用Ajax代替完全的刷新页面。
  4. 删除一个评论,但不丢失它的子评论。。。。
  5. 给嵌套评论添加pagination。

视频1

https://gorails.com/episodes/nested-comment-threads-in-rails-part-1?autoplay=1

目标:建立数据库结构,和嵌套视图。

方法概览:

1. 建立一个可以自我关联的comment的表结构。

2. 建立partial模版_comment和_form

3. 设置routes, 出现post/1/comments/2这样的url

4. 新建controller:

 class Posts::CommentsController < CommentsController
//内部添加before_action :set_commentable, 通过params[:post_id]找到对应的@post实例。

5. 新建CommentsController类,增加create方法

6. 在show页面添加form渲染和comment渲染

7. 基本结构已经做出来了,下面进行嵌套评论的设计,代码在视图层完成。

在comment模版上增加form模版:

  • 在_form上添加parent_id的输入框<input>,parent_id自动得到值,因此设置为不可见type='hidden'
  • 子评论form默认不可见,需要点击‘回复reply’才出现。comment模版增加这个功能。这里使用Stimulus来取元素(reply 链接)并绑定click事件(添加/移除 form模版)

8. 需要在父评论的下面显示新增子评论。而不是直接显示在所有评论最下方。因此需要修改show视图。

  • show视图只显示非子评论。即渲染comment时,增加一个筛选parent_id: nil。
  • 在comment模版中,渲染出当前评论的子评论<%= render comment.comments %>
  • 还是comment模版,它渲染的form模版,传入的变量改为comment.commentable。
rails new -m template.rb nested_coments

cd nested_comments

rails g scaffold Post title body:text

rails g model Comments user:references commentable:references{polymorphic}:index parent_id:integer body:text

rails db:migrate
解释:
commentable:references{polymorphic}:index 用于一个model belongs_to从属多个models.即Polymorphic.
生成了commentable_type:string, commentable_id:integer,以及这两个列组成的index.
class Post < ApplicationRecord
has_many :comments, as: :commentable
end class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
belongs_to :parent, optional: true, class_name: "Comment"
//optional和class_name用于内部Comment关联,并且这是可选的无需存在验证。✅ def comments
//搜索某个post下的一个父评论的所有它的子评论。
//搜索条件1: comment记录的commentable_type和commentable_id指向post,
//因为添加了commentable的index索引,所以使用commentabel: commentable
//搜索条件2: comment记录的子评论,用parent_id即可得到。
 Comment.where(commentable: commentable, parent_id: id)
 end
end

解释Polymorphic 关联:

一个model可以属于belongs_to多个其他model。

commentable_id, commentable_type已经由model产生。

@comment.commentable_id是@post的

Nested Comment Treads in ROR的更多相关文章

  1. 前端备忘录 — IE 的条件注释

    CSS hack 由于不同厂商的浏览器,比如 Internet Explorer,Safari,Mozilla Firefox, Chrome 等,或者是同一厂商的浏览器的不同版本,如 IE6 和 I ...

  2. javascript 之正则匹配HTML

    正则表达式 <(\S*?) [^>]*>.*?</\1>|<.*? /> 匹配 <html>hello</html>|<a> ...

  3. ccs6.0使用问题记录

    ccs6.0使用问题记录 彭会锋 1 编译过程中提示warning  " Description Resource Path Location Type #9-D nested commen ...

  4. 关于IE条件注释(译)

    本文翻译自此篇文章.翻译纯属业余. 许多网站为了确保他们的站点能够在不同的浏览器上有不同的显示效果而使用特征检测,一些传统的网站使用其他技术,诸如在服务器或客户端上使用脚本去检测浏览器类型.在这里我们 ...

  5. 统计C/C++代码行数

    近日在写一个统计项目中C/C++文件(后缀名:C/CPP/CC/H/HPP文件)代码行数的小程序.给定包含C/C++代码的目录,统计目录里所有C/C++文件的总代码行数.有效代码行数.注释行数.空白行 ...

  6. 算法语言Scheme修订6报告 R6RS简体中文翻译

    算法语言Scheme修订6报告 R6RS简体中文翻译 来源 https://r6rs.mrliu.org/   MICHAEL SPERBERR. KENT DYBVIG, MATTHEW FLATT ...

  7. CocoSourcesCS 2

    CocoSourcesCS 2 /*------------------------------------------------------------------------- DFA.cs - ...

  8. Css:Conditional comments 条件注释

    http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx http://www.quirksmode.org/css/condcom.h ...

  9. 【转帖】从 Oracle 到 PostgreSQL ,某保险公司迁移实践 技术实践

    从 Oracle 到 PostgreSQL ,某保险公司迁移实践 http://www.itpub.net/2019/11/08/4108/ 信泰人寿保险股份有限公司 摘要:去O一直是金融保险行业永恒 ...

随机推荐

  1. dart实例

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  2. 在fedora23中安装virtualbox, 然后实现虚拟机irtualbox 或者 vmware 下的xp操作系统

    参考: http://blog.csdn.net/statdm/article/details/7756788 参考: http://www.cnblogs.com/fengbohello/p/488 ...

  3. Windows,远程计算机:X.X.X.X,这可能是由于CredSSP加密Oracle修正

    https://blog.csdn.net/wyhwlls/article/details/80320301 近期window 10家庭版更新后,远程桌面连不到服务器了 网上有卸载补丁,修改组策略什么 ...

  4. 搭建git 服务器

    Gogs 什么是 Gogs? Gogs 是一款极易搭建的自助 Git 服务. https://gogs.io/docs

  5. 论文笔记:A Review on Deep Learning Techniques Applied to Semantic Segmentation

    A Review on Deep Learning Techniques Applied to Semantic Segmentation 2018-02-22  10:38:12   1. Intr ...

  6. Install and Compile MatConvNet: CNNs for MATLAB --- Deep Learning framework

    Install and Compile MatConvNet: CNNs for MATLAB --- Deep Learning framework 2017-04-18  10:19:35 If ...

  7. 消息队列之ActiveMQ简单环境搭建

    准备: 环境:win7,Eclipse,jdk1.8 ActiveMQ版本:ActiveMQ 5.9.0 Release下载地址:http://activemq.apache.org/download ...

  8. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  9. Linux学习之用户与root

    因为想要建立建立一个目录,但是发现权限不够,因为没用root登陆,所以学习了一下普通用户与root之间如何切换以及如何创建用户的一些知识. 1.pwd命令可以查看当前用户 $这个符号代表的就是普通用户 ...

  10. HTML XHTML HTNL5 简介

    XHTML 是HTML与XML(扩展标记语言)的结合物 包含了所有与XML语法结合的HTML 4.01元素 XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup ...