繁体   English   中英

ActionController::UnknownFormat 尝试将 .js 文件添加到我的 Rails 应用程序时

[英]ActionController::UnknownFormat when try to add .js file to my rails app

我试图在我的 rails 应用程序上获取一些 .js,但我有 1 个问题。

已经有一个相同的问题,但答案对我来说也不好。

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'

TL;DR 删除 format.js 之后的重定向

你不能用JS响应然后重定向。 重定向用于 HTTP 请求。 删除重定向应该可以解决您的问题。 如果您还想处理 HTTP 请求,请在重定向前添加format.html

如果你想重定向,你可以将重定向 URL 传递到你的 JS 中,然后从那里用 window.location 或其他东西操纵 URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM