Question

I have a problem with my remote form in rails 4 using simple form. It works perfectly when I use it without AJAX but as soon as I set remote to true rails throws an ActionDispatch::ParamsParser::ParseError saying unexpected token at 'utf8=%E2%9C%93&...'.

I have no clue, what could be causing this but it's probably something really stupid, since I seem to be the only one having it :)

This is the form tag I'm using:

<%= simple_form_for @profile, remote: true, defaults: { wrapper_html: {:class => 'form-group'}, input_html: { :class => 'form-control' } } do |f| %>

And this is the trace rails returns to me:

actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:23:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__732810880914274126__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:138:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:94:in `run'
webrick (1.3.1) lib/webrick/server.rb:191:in `block in start_thread'

Thank you very much for your help!

Was it helpful?

Solution

It might not be many, but for everyone who accidentally runs into the same problem as me, I finally found the solution: Somewhere deep in my codebase someone set global settings for jQuery Ajax requests, setting the content-type to JSON. Since Rails uses the jQuery Ajax helpers but is not using JSON (but normal query strings) for it's remote forms the Rails parser of course detected malformed JSON and threw the error. Removing those global settings solved the problem.

OTHER TIPS

TL,DR just remove contentType: "application/json",

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top