[英]Rails ActionController::UnknownFormat error with respond_to
i'm getting this error, when trying to use ajax.尝试使用ajax时出现此错误。 I did some searching, but couldn't solve my problem
我做了一些搜索,但无法解决我的问题
ActionController::UnknownFormat
Drafts 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 index.js.erb
$('.draft_<%= @draft.id %>').trigger('click');
log:日志:
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'
Create Method.创建方法。 This loads the index with params["format"]
这将使用 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 where create method is called调用 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"
For one, you're still specifying instance variables after your implicit call to render
(in the respond_to
block).一方面,您仍然在隐式调用
render
之后指定实例变量(在respond_to
块中)。 Those instance variable declarations should be above any rendering.这些实例变量声明应该在任何渲染之上。
Also, I've seen this happen in some browsers.此外,我已经在某些浏览器中看到了这种情况。 You can get around it by declaring a dummy render block for the
html
type, so something like:您可以通过为
html
类型声明一个虚拟渲染块来解决它,例如:
respond_to do |format|
format.html { render(:text => "not implemented") }
format.js
end
The html
format block will likely never get called, but its declared nonetheless. html
格式块可能永远不会被调用,但它仍然声明。
You need to add remote: true
to the form helper since you are trying to submit the form using ajax.您需要将
remote: true
添加到表单助手,因为您正在尝试使用 ajax 提交表单。
= 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"
You must ensure that your request are passing the correct headers.您必须确保您的请求传递正确的标头。
In my case I'm on a rails API and my rspec test was calling the "default" type and not the implemented ones (json or csv).就我而言,我使用的是 Rails API,我的 rspec 测试调用的是“默认”类型,而不是已实现的类型(json 或 csv)。 So I got the described error above.
所以我得到了上面描述的错误。
To solve this I've just set the request headers in my tests and everything worked.为了解决这个问题,我刚刚在我的测试中设置了请求标头,一切正常。
This problem happened with me and sovled by just add这个问题发生在我身上,只需添加即可解决
respond_to :html, :json
to application_controller.rb file到 application_controller.rb 文件
You can Check Devise issues on Github: https://github.com/plataformatec/devise/issues/2667你可以在 Github 上查看设计问题: https : //github.com/plataformatec/devise/issues/2667
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.