2015-12-02 174 views
0

所以我试图用AJAX更新我的评论部分,而没有整个页面刷新大学项目。不过,我似乎无法得到这个工作。 在它给我的控制台ŠRuby on rails AJAX提交表单错误

无法加载资源:

<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> 
:服务器500(内部服务器错误)的状态

我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但是我只需要现在显示形式。

+0

检查控制台是否有错误。请在这里发布您的错误。与500它必须提供一些更多的错误日志.. –

+0

我从字面上只是更新:) – StormViper

+0

在这个远程应该创建'create.js.erb'而不是'create.html.erb' –

回答

3

我注意到你正在张贴到URL post_comments_path(@post)。对于嵌套的路线,它可能是清洁剂做到以下几点:

1)后,直接嵌套的路线:

<%= form_for([@post, @comment], html: {class: "form", role: "forms"}, remote: true) do |comm| %> 

2)确保您的路线正确嵌套,在routes.rb中:

resources :posts do 
    resources :comments 
end 

3)重构您CommentsController,建立通过@post实例:

class CommentsController < ApplicationController 

    before_action :get_post 

    def index 
    @comments = @post.comments 
    end 

    def new 
    @comment = @post.comments.build  
    end 

    def create 
    @comment = @post.comments.build(comment_params) 

    if @comment.save 

     flash[:success] = "Successfully created comment" 

     respond_to do |format| 
     format.html { redirect_to post_path(@post) } 
     format.js 
     end 

    else 
     respond_to do |format| 
     format.html { render :new } 
     format.js { render :error } 
     end  
    end 
    end 

    private 

    def comment_params 
    params.require(:comment).permit(:body) 
    end 

    def get_post 
    @post = Post.find(params[:post_id]) 
    end 

end 

4)重新在app/views/comments/error.js.erb的验证错误。我会让你决定如何最好地做到这一点,但这里有一个快速转储到控制台:

<% @comment.errors.full_messages.each do |message| %> 
    console.log("<%= message %>"); 
<% end %> 

这个文件是不是与您应用程序/意见/评论/ create.js.erb困惑这是处理成功保存的评论。这应该看起来像这样:

$('#comments').append("<%= j render @comment %>"); 
$(".form")[0].reset(); 

5)调整你的看法一点点。我注意到你需要输出的意见不同:

<ol id="comments"> 
    <%= render @post.comments %> 
</ol> 

应该对应的部分在应用程序/意见/评论/ _comment.html。erb所以确保这里有。

+0

我已经完成了所有这一切,似乎没有任何工作 – StormViper

+0

你正在得到什么500错误,具体来说....你可以看看你的导轨服务器的输出日志,看到确切的错误?即不是Javascript控制台中的错误消息。 – rlarcombe

+0

'缺少模板注释/创建,使用{:locale => [:en],:formats => [:js,:html],:variants => [],:handlers => [:erb,: builder,:raw,:ruby,:coffee,:jbuilder]}。搜索: *“/ home/adam/Desktop/ruby​​/rails/AJAXC/app/views”

'
这是我得到的错误 – StormViper