Rails中ActionController::UnknownFormat错误和respond_to相关

5

当我尝试使用ajax时,出现了以下错误。我进行了一些搜索,但无法解决我的问题。

ActionController::UnknownFormat

草稿控制器:

  def index
    if params["format"] != nil
      @draft = Draft.find_by(id: params["format"].to_i)
      respond_to do |format|
        format.js
      end
    end
    @draft = current_user.drafts.build
    @drafts = current_user.drafts.non_submitted
    @being_edited_drafts = current_user.drafts.being_edited
    @completed_drafts = current_user.drafts.completed
  end

index.js.erb

$('.draft_<%= @draft.id %>').trigger('click');

日志:

Started GET "/drafts.132" for 127.0.0.1 at 2014-11-26 11:56:28 -0800
Processing by DraftsController#index as 
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 4  ORDER BY     "users"."id" ASC LIMIT 1
  Draft Load (0.4ms)  SELECT  "drafts".* FROM "drafts"  WHERE "drafts"."id" = 132 LIMIT 1
Completed 406 Not Acceptable in 5ms

ActionController::UnknownFormat - ActionController::UnknownFormat:
actionpack (4.1.7) lib/action_controller/metal/mime_responds.rb:440:in   `retrieve_collector_from_mimes'
actionpack (4.1.7) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/drafts_controller.rb:16:in `index'
actionpack (4.1.7) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.7) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.7) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.7) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'

创建方法。这会使用params["format"]加载索引。
def create
  @draft = Draft.create(draft_params)
  if @draft.save
    redirect_to drafts_path(@draft.id),notice: "Draft was successfully saved"
  else
    render action: 'new'
  end
end

创建方法被调用的表单

= form_for @draft, :html => { :multipart => true } do |f|
  = f.hidden_field :user_id, value: current_user.id
  #new-post-title
    = f.text_field :title, :value => "<h1><b>Title</b></h2>"

  #new-post-body
   = f.text_area :body, :value => "<p>Start writing</p>"

  .text-center
   = f.submit "New Draft", :class => "btn btn-sm btn-primary btn-round"

1
这发生在“索引”还是“创建”上吗? - Uri Agassi
是的,@UriAgassi,好问题。如果你想让你的create操作响应JavaScript,那么确实需要一个create.js.erb文件来对应处理。不过,如果你真的希望你的index操作响应JavaScript,那么你就需要一个相应的index.js.erb文件。 - Jake Smith
对不起,它被称为 index.js.erb,我写错了文件名。 - Jay
请添加服务器日志跟踪。这将对回答问题的人非常有帮助,也很容易实现。 - Rubyrider
1
在5毫秒内完成406不可接受的请求!! 响应类型未定义!!这与MIME类型有关,可能是误导或需要检查您发送表单的位置!能否分享该脚本和表单部分或链接? - Rubyrider
Rubyrider,我添加了表单和创建方法,该方法使用params["format"]呈现索引。 - Jay
4个回答

8
首先,在隐式调用render(在respond_to块中)之后,您仍然在指定实例变量。这些实例变量声明应该在任何渲染之前。
另外,我在一些浏览器中看到过这种情况。您可以通过为html类型声明一个虚拟的渲染块来解决这个问题,例如:
respond_to do |format|
  format.html { render(:text => "not implemented") }
  format.js
end

html 格式块可能永远不会被调用,但它仍然被声明。


这对我在Rails 6中有效! - developer01

5

由于您正在尝试使用ajax提交表单,因此需要在表单帮助程序中添加remote: true

= form_for @draft, :html => { :multipart => true }, remote: true do |f|
  = f.hidden_field :user_id, value: current_user.id
  #new-post-title
    = f.text_field :title, :value => "<h1><b>Title</b></h2>"

  #new-post-body
   = f.text_area :body, :value => "<p>Start writing</p>"

  .text-center
   = f.submit "New Draft", :class => "btn btn-sm btn-primary btn-round"

0

您必须确保您的请求传递了正确的头信息。

在我的情况下,我正在使用Rails API,我的rspec测试调用了“默认”类型而不是已实现的类型(json或csv)。 因此,我遇到了上述描述的错误。

为了解决这个问题,我只需在我的测试中设置请求头信息,一切都正常工作了。


-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接