我正在尝试在我的rails应用程序上获取一些.js,但我有一个问题。
已经有一个相同的问题,但答案对我也不好。
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(params[:comment].permit(:name, :body))
@comment.name = current_user.email
respond_to do |format|
if @comment.save
format.js
redirect_to article_path(@article)
else
end
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
end还有Create.js.erb文件
$('#forma2 table').append("<%= j render @comment %>")和来自终端的错误
Redirected to http://localhost:3000/articles/90
Completed 406 Not Acceptable in 27ms (ActiveRecord: 10.5ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/comments_controller.rb:7:in `create'发布于 2020-01-10 08:08:22
TL;DR删除format.js之后的重定向
您不能使用JS进行响应,然后重定向。重定向是针对HTTP请求的。删除重定向应该可以解决您的问题。如果您还想处理HTTP请求,那么在重定向之前添加format.html。
如果想要重定向,可以将重定向URL传入JS,然后在那里用window.location或其他方法操作URL。
https://stackoverflow.com/questions/59673746
复制相似问题