升级到Rails 4.2.9后,Ajax不起作用(格式:: js)

时间:2017-08-24 06:59:52

标签: ajax request format ruby-on-rails-4.2

代码

我刚刚将Rails从4.1.11升级到4.2.9,除了ajax,一切正常!

我有一个链接与ajax一起使用,如下所示:

<%= link_to("hide", admin_course_path(id: course.id, hide: "true"), remote: true, format: :js, method: :put) %>

后端看起来像这样:

class Admin::CoursesController < .....
  respond_to :html, :json, :js

  def update
    @course = Course.find(params[:id])

    if params[:hide]
      @course.update_attributes(hide: true)
    else
      .....
    end

    respond_with([:admin, @course])
  end
end

问题

它应该是一个js ajax,但现在我收到如下错误:

Started PUT "/admin/courses/11111?hide=true" for ::1 at 2017-08-24 12:57:49 +0800
Processing by Admin::CoursesController#update as JS
  Parameters: {"hide"=>"true", "id"=>"11111"}
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 21808  ORDER BY `users`.`id` ASC LIMIT 1
  Course Load (0.4ms)  SELECT  `courses`.* FROM `courses` WHERE `courses`.`id` = 11111 LIMIT 1
Completed 500 Internal Server Error in 17ms (ActiveRecord: 2.1ms)

NoMethodError - undefined method `ref' for nil:NilClass:
  actionpack (4.2.9) lib/action_controller/metal/rendering.rb:9:in `process_action'
  actionpack (4.2.9) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.9) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.9) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.9) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.9) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.9) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.9) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.9) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.9) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.9) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.9) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.9) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.9) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.9) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.9) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:817:in `call'

问题可能出在actionpack (4.2.9) lib/action_controller/metal/rendering.rb:9:in process_action行。

深入挖掘actionpack后,我发现process_action定义为:

# Before processing, set the request formats in current controller formats.
def process_action(*) #:nodoc:
  self.formats = request.formats.map(&:ref).compact
  super
end

所以这意味着来自ajax的请求无法获得格式。 我继续打印 request.format &amp; request.formats ,第一个显示 text / javascript ,但第二个没有显示任何内容。 我不知道为什么缺少 request.formats

我还将jquery-rails升级到4.1.1(与ajax相关的那个),但没有运气。

如果有人知道这一点,请提供一些建议。非常感谢:)

信息

  • rails:4.2.9
  • jquery-rails:4.1.1
  • 响应者:2.4.0

1 个答案:

答案 0 :(得分:0)

经过长时间的测试后,我发现你应该在路径助手中添加 format :: js

<%= link_to("hide", admin_course_path(id: course.id, hide: "true", format: :js), remote: true, method: :put) %>

它会自动转换为/admin/course/:id.js,并且不会导致错误!