Reputation: 21
I just upgraded Rails from 4.1.11 to 4.2.9, and everything works fine except ajax!
I got a link works with ajax which looks like below:
<%= link_to("hide", admin_course_path(id: course.id, hide: "true"), remote: true, format: :js, method: :put) %>
And backend looks like this:
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
It should be a js ajax, but now I receive error like below:
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'
Maybe the problem is at the line actionpack (4.2.9) lib/action_controller/metal/rendering.rb:9:in process_action
.
After digging into actionpack deeper, I found that the process_action defined as:
# Before processing, set the request formats in current controller formats.
def process_action(*) #:nodoc:
self.formats = request.formats.map(&:ref).compact
super
end
So it means the request from ajax can't get the formats. I went on to print request.format & request.formats, first one showed text/javascript, but second one showed nothing. I don't know why the request.formats is missing.
I also upgrade jquery-rails to 4.1.1(the one that related to ajax), but no luck.
If anyone know about this, please give some advice. Much appreciated:)
Upvotes: 0
Views: 197
Reputation: 21
After a long period of testing, I found that you should add format: :js in the path helper.
<%= link_to("hide", admin_course_path(id: course.id, hide: "true", format: :js), remote: true, method: :put) %>
It will transform to /admin/course/:id.js
automatically, and cause no errors!
Upvotes: 0