2015-12-02 172 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