[英]ActionController::UnknownFormat when try to add .js file to my rails app
I am trying to get some .js on my rails app and I have 1 problem.我试图在我的 rails 应用程序上获取一些 .js,但我有 1 个问题。
There is already one same question, but answer is not good for me also.已经有一个相同的问题,但答案对我来说也不好。
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
There is also Create.js.erb file还有 Create.js.erb 文件
$('#forma2 table').append("<%= j render @comment %>")
And error from terminal来自终端的错误
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'
TL;DR Remove the redirect after format.js TL;DR 删除 format.js 之后的重定向
You can't respond with JS and then redirect.你不能用JS响应然后重定向。 Redirects are for HTTP requests.
重定向用于 HTTP 请求。 Removing the redirect should fix your issue.
删除重定向应该可以解决您的问题。 If you want to handle HTTP requests as well then add
format.html
before the redirect.如果您还想处理 HTTP 请求,请在重定向前添加
format.html
。
If you want to redirect you can pass in the redirect URL into your JS and from there manipulate URL with window.location or something.如果你想重定向,你可以将重定向 URL 传递到你的 JS 中,然后从那里用 window.location 或其他东西操纵 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.