[英]Rails Ajax 500 Server Error
我正在嘗試獲取新的評論以用ajax更新,而不是重新加載頁面。 我遵循了railscast教程,但是在我的js控制台中收到500個內部服務器錯誤。 我在閱讀其他文章,很多人說這是部分錯誤,但我無法弄清楚。 注釋將在頁面重新加載之前保存,但是直到頁面重新加載后才會顯示。
def create
@post = Post.find(params[:post_id])
@comment = Comment.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.post_id = @post.id
if @comment.save
respond_to do |format|
format.html {redirect_to post_path(@post.comments)}
format.js
end
else
render 'new'
end
end
評論目錄中的create.js.erb文件。
$('.comment').append('<%= j render @post.comments %>');
評論表
<%= simple_form_for([@post, @post.comments.build], :remote => true, :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
我不確定100%是否是您當前遇到的問題,但是format.html {redirect_to post_path(@post.comments)}
試圖轉到屬於某個帖子的所有評論的post
路徑,但這並不能感。 我認為您的意思是去該post
的路。
format.html { redirect_to post_path(@post) }
如果這是部分問題,那么這里是一個堆棧溢出問題 ,可能會有所幫助。
也有可能您在部分代碼上犯了同樣的錯誤,應該將其傳遞給@post
而不是@post.comments
。 或者,也許您應該將最新評論而不是所有評論傳遞給它。
編輯:
您可能需要在javascript中指定局部變量:
$('.comment').append('<%= j render 'comments/form', locals: {post: @post} %>');
在您的評論表中,我相信您@post
所有@post
更改為post
。
編輯2:
試試看 這和我上一次編輯之間的區別在於,現在您將表單放在帖子上,而不是將表單放在評論上。
# app/controllers/comments_controller.rb
def create
@post = Post.find(params[:post_id])
@comment = Comment.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.post_id = @post.id
if @comment.save
respond_to do |format|
format.html { redirect_to post_path(@post) }
format.js
end
else
render 'new'
end
end
# app/views/posts/show.html.erb
# Doesn't need to look exactly like this
<div class='post'>
<div class='content'>
<%= @post.content %>
</div>
<div class='form'>
<%=j render 'comments/form', locals: { post: @post } %>
</div>
</div>
<div class='comment'>
<%= render @post.comments %>
</div>
# app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>');
# app/views/comments/_comment.html.erb
<%= comment.content %>
# app/views/posts/_form.html.erb
<%= simple_form_for([@post, @post.comments.build], :remote => true, :input_html => { :data => { :url => '/post/:id', :type => :js } }) do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
#config/routes.rb
resources :posts do
resources :comments #-> url.com/posts/:post_id/comments/new
end
#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
respond_to :js, :html, only: :create #-> requires "responders" gem
def create
@post = Post.find params[:post_id]
@comment = @post.comments.new comment_params
respond_with @comment
end
private
def comment_params
params.require(:comment).permit(:content).merge(user_id: current_user.id)
end
end
#app/views/comments/create.js.erb
$('.comment').append('<%=j render @post.comments %>'); #-> requires comments/_comment.html.erb
#app/views/comments/_comment.html.erb
<%= comment.content %>
#app/views/posts/show.html.erb
<%= simple_form_for [@post, @post.comments.new], remote: true do |f| %>
<%= f.input :content, label: "Reply"%>
<%= f.button :submit, "Submit" %>
<% end %>
以上是它應該如何工作的。
我把所有內容寫出來是為了提供適當的上下文等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.