gpt4 book ai didi

ruby-on-rails - Rails ActionController::UnknownFormat 错误与 respond_to

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:48 26 4
gpt4 key购买 nike

我在尝试使用 ajax 时遇到此错误。我做了一些搜索,但无法解决我的问题

ActionController::UnknownFormat

草稿 Controller :

  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

调用create方法的表单

= 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"

最佳答案

一方面,您仍然在隐式调用 render 之后指定实例变量(在 respond_to block 中)。这些实例变量声明应该在任何渲染之上。

此外,我在某些浏览器中也看到过这种情况。您可以通过为 html 类型声明一个虚拟渲染 block 来绕过它,例如:

respond_to do |format|
format.html { render(:text => "not implemented") }
format.js
end

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

关于ruby-on-rails - Rails ActionController::UnknownFormat 错误与 respond_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27157477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com