简体   繁体   English

为什么request.format是text / javascript,而response_to format说不是呢?

[英]why would request.format be text/javascript while respond_to format says it's not?

Rails 3.1.1 Rails 3.1.1

I'm setting up a dialog that is supposed to post back to the server via ajax, then show a 'done' message when it's completed. 我正在建立一个对话框,该对话框应该通过ajax发布回服务器,然后在完成时显示一条“完成”消息。

  <%= form_tag '/tracking/add',  :remote => true, :id => 'add_tracking_request', 
      :html => {:'data-type' => 'json'} do %>
  ....

Ok so that's getting back to the server, doing all of its work etc. and then getting to respond_to: 好的,现在回到服务器,完成所有工作等,然后返回respond_to:

   135          puts 'returning ...'
   136          if current_user
   137            respond_to do |format|
   138              debugger
=> 139              format.html { redirect_to :myTrackingRequests and return }
   140              format.js { head :ok, :success => 'true' }
   141            end
   142          else
   143            raise 'no user found at the end of create tracking'
(rdb:67) e format
#<ActionController::MimeResponds::Collector:0x007f98d63417c0 @default_response=#<Proc:0x007f98d6341720@/Users/user/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.1.1/lib/action_controller/metal/mime_responds.rb:268>, @responses={}, @order=[]>
(rdb:67) e format.html
nil
(rdb:67) e format.js
nil
(rdb:67) e format.json
nil
(rdb:67) request.format
text/javascript
(rdb:67) request.post? 
true
(rdb:67) 

I'm in a position where the object passed to respond_to block says it is not html, js or json, but the request is a post and its format is text/javascript. 我处于传递给response_to块的对象说不是html,js或json的位置,但是请求是帖子,其格式是text / javascript。 I'm not sure what I'm missing here. 我不确定我在这里缺少什么。

<%= form_tag '/tracking/add',  :remote => true, :id => 'add_tracking_request', 'data-type' => 'json' do %>

The format object you get in the respond_to block is just simple container (Collector) that collects responses. 您在response_to块中获得的format对象只是用于收集响应的简单容器(Collector)。 It returns nil for every type because the script haven't reached the point where they are actually defined. 对于每种类型,它都返回nil ,因为脚本尚未达到实际定义的程度。

See the implementation here ActionController::MimeResponds::Collector . 在此处查看实现ActionController :: MimeResponds :: Collector

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

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