所以我试图用AJAX更新我的评论部分,而没有整个页面刷新大学项目。不过,我似乎无法得到这个工作。 在它给我的控制台ŠRuby on rails AJAX提交表单错误
无法加载资源:
:服务器500(内部服务器错误)的状态<h1>Title: <%= @post.title %></h1> <h2>Body: <%= @post.body %></h2> <hr /> <h1>Your comments</h1> <%= link_to "View comments", "#", id: "comments-link" %> <ol id="comments"> <%= render 'comments' %> <hr /> <h1>Create comment</h1> <%= form_for(@comment, :html => {class: "form", role: "forms"}, :url => post_comments_path(@post), remote: true) do |comm| %> <div class="container"> <div class="input-group input-group-md"> <%= comm.label :body %> <%= comm.text_area :body, class: "form-control", placeholder: "Enter a comment" %> </div> <%= comm.submit "custom", id: "button" %> </div> <% end %> </ol>
我show.html.erb文件回应
我comments.coffee:
$(document).on "page:change", ->
$('#comments-link').click ->
$('#comments').fadeToggle()
$('#comments_body').focus()
我create.js .erb:
$('#comments').append("<%= j render @comment %>");
和我的意见控制器:
class CommentsController < ApplicationController
def index
end
def new
end
def new
@comment = Comment.new
end
def create
@comment = Comment.new(comment_params)
@comment.post_id = params[:post_id]
if @comment.save
flash[:success] = "Successfully created comment"
respond_to do |format|
format.html { redirect_to post_path(@comment.post_id) }
format.js
end
else
flash[:danger] = "Failed to create comment"
redirect_to root_path
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
我可能已经错过了一些文件,所以才让我知道,这是基本的,因为它只是一个岗位和评论系统 - 无需为造型该项目,所以是的。过去4个小时我一直在尝试,其他地方都不行。我在这里看过,Youtube - 无处不在,但是没有其他人的代码适合我,所以我来到了这里!感谢您的帮助。
编辑:
我注意到它说创造的错误响应观点,但是我做了这个观点,并呈现评论的身体到create.html.erb但是我只需要现在显示形式。
检查控制台是否有错误。请在这里发布您的错误。与500它必须提供一些更多的错误日志.. –
我从字面上只是更新:) – StormViper
在这个远程应该创建'create.js.erb'而不是'create.html.erb' –