我整天都在网上搜索这个问题的解决方案。堆栈溢出的很多人都有同样的问题,我已经尝试了所有的解决方案,但它似乎并不适合我,所以我想我会在这里试一试。我只是试图让我的页面在发布后更新我的评论而不刷新。问题似乎在于我的create.js.erb文件,因为它似乎根本没有注册。这是我到目前为止所做的。
控制器:
class CommentsController < ApplicationController
def create
@content = params[:content]
@comment = Comment.create(:video_id => params[:video_id], :content => params[:content])
@video = Video.find(params[:video_id])
respond_to do |format|
format.html {redirect_to "/videos/#{@video.id}", flash:{success_comment: "created!"}}
format.js {}
end
end
end
创建评论表:
<%= form_tag "/videos/#{@video.id}/comments", :method => "post", :remote => true do %>
<%=label_tag :comment, nil %>
<%=text_area_tag :content, nil, class: "commentinput" %>
<%=submit_tag "Comment"%>
<% end %>
app / views / comments / create.js.erb:
$('.comments').append('<%= escape_javascript(render :partial => @comment) %>');
$('.commentinput').val('')
有人可以告诉我这里缺少什么吗???提前谢谢。
答案 0 :(得分:1)
Rails说当在form_tag上使用remote:true时,你需要自然地为你的元素/应用程序量身定制这一小段代码。你试过了吗?
$ ->
$("a[data-remote]").on "ajax:success", (e, data, status, xhr) ->
alert "The article was deleted."
答案 1 :(得分:0)
Rails服务器终端窗口或日志文件中的任何错误消息?...
调整你的create.js.erb文件进行测试:
$('.comments').append('<%= escape_javascript(render :text => 'the create.js.erb file seems to work') %>');
...应该在每条评论下添加该文本,并演示代码的一般结构是否正常(如果不是......其他错误!)。
然后,看一下render
函数的语法 - 是否有一个_comment.html.haml
(或.erb
)部分会绘制一个注释行?
如果是这样,您应该能够将您的行更改为:
$('.comments').append('<%= escape_javascript(render :partial => 'comment') %>');
或
$('.comments').append('<%= escape_javascript(render @comment) %>');