【问题标题】:ActionController::UnknownFormat when try to add .js file to my rails app尝试将 .js 文件添加到我的 Rails 应用程序时的 ActionController::UnknownFormat
【发布时间】:2020-01-09 23:52:56
【问题描述】:

我正在尝试在我的 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'

【问题讨论】:

    标签: javascript ruby-on-rails ruby forms


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多