I'm fairly new to Ruby on rails or coding in general. I've been following this tutorial to build a commenting system on my Ruby on rails web app using acts-as-commentable-with-threading-gem.

我对Ruby on rails或编码很新。我一直在关注本教程,使用acts-as-commentable-with-threading-gem在我的Ruby on rails web应用程序上构建一个评论系统。

I'm trying to ajaxify "Post" and "Reply" actions so that the new comments get appended to the page without having to reload. But it results in the following error:

我正在尝试ajaxify“发布”和“回复”操作,以便新注释可以附加到页面而无需重新加载。但它会导致以下错误:

"ActionView::MissingTemplate - Missing partial comments/_comment with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :jbuilder]}."

I've been trying to figure this out but no luck so far. I've modified the "def create" part of the comments controller as follows:

我一直试图弄清楚这一点但到目前为止没有运气。我已经修改了注释控制器的“def create”部分,如下所示:

def create
  commentable = commentable_type.constantize.find(commentable_id)
  @comment = Comment.build_from(commentable, current_user.id, body)
  @comments = Comment.where(commentable_id: commentable_id)

  respond_to do |format|
   if @comment.save
     make_child_comment
     format.html { redirect_to(:back) }
     format.js
   else
     format.html { render :action => "new" }
     format.js
   end
  end
end

and created a views/comments/create.js.erb file with the following code:

并使用以下代码创建了views / comments / create.js.erb文件:

$('.comments-container').prepend("<%= j render @comments %>");

and of course modified the form actions in form as well as reply templates to respond to asynchronous requests as:

当然,修改了表单中的表单操作以及回复模板,以响应异步请求:

_reply.html.erb line:7

<%= form_for @new_comment, remote: true do |f| %>

_form.html.erb line:1

<%= form_for @new_comment, remote: true do |f| %>

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


0

Since you're new, I'll give you some info, although I've not used acts-as-commentable-with-threading before.

既然你是新手,我会给你一些信息,虽然我之前没有使用过act-with-commentable-with-threading。


ActionView::MissingTemplate - Missing partial comments/_comment

ActionView :: MissingTemplate - 缺少部分注释/ _comment

The error basically means you don't have the app/views/comments/_comment.html.erb partial.

该错误基本上意味着您没有app / views / comments / _comment.html.erb partial。

The problem is here:

问题出在这里:

<%= j render @comments %>

--

As per the docs:

根据文档:

When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is _product, and within the _product partial, you can refer to product to get the instance that is being rendered.

当使用复数集合调用partial时,partial的各个实例可以访问通过以partial命名的变量呈现的集合的成员。在这种情况下,partial是_product,在_product partial中,您可以引用product来获取正在呈现的实例。

This means that if you're calling render @comments, Rails is going to look up the _comment.html.erb partial. Because it cannot find it, your error appears.

这意味着如果你正在调用render @comments,Rails会查找_comment.html.erb部分。因为找不到它,所以会出现错误。

The fix is to move your comment code into a partial, and use that to populate your comments:

修复方法是将您的注释代码移动到部分代码中,并使用它来填充您的注释:

#app/views/comments/show.html.erb
<%= render @comment %>

#app/views/comments/_comment.html.erb
<%= comment.title %> #-> put your comment code here

This should fix your error.

这应该可以解决您的错误。

更多相关文章

  1. Chrome显示错误为:由于内容安全策略,拒绝执行内联脚本
  2. Java applet错误…怎么了?
  3. 2003生成 注释web页问题,第二级的html 内容为空? 为什么?
  4. Jquery - 表单验证,为错误消息添加css样式
  5. jQuery验证没有错误计数
  6. 将我的代码中的JavaDoc注释转换为HTML
  7. GET错误Glyphicon-halflings-regular.ttf / woff / woff2与来自L
  8. iis aspx常见错误 HTTP 错误 404 - 文件或目录未找到。
  9. 在HTML / PHP联系表单上出现404/405错误(链接问题?)

随机推荐

  1. Android 驱动之旅 第四章:在Android 系统
  2. Android(安卓)音视频深入 四 Android原生
  3. Android游戏可能遇到的3个问题及解决方案
  4. Android(安卓)三言两语
  5. Android交互体验必知:功能按键事件
  6. 一种绕过Android P对非SDK接口限制的简单
  7. Android webview注入自己的js代码(js传入f
  8. Android(安卓)MVP 模式:简单易懂的介绍方
  9. 第二部分:开发简要指南-第五章 支持不同的
  10. 关于android单元测试结合SAX解析xml文件