Activeadmin stops my jQuery working

2020-07-06 06:57发布

I'm using jquery drag and drop in my app and it works fine.

I then added activeadmin and it stops my jquery working.

I get this error

$(".draggable_article_image").draggable is not a function

If I remove this line from active_admin.js

//= require active_admin/base

it starts working again.

Any ideas?

3条回答
仙女界的扛把子
2楼-- · 2020-07-06 07:28

Try moving your active_admin.js file to the vendor/assets/javascripts folder of your Rails project : you should be fine.

Please let us know if it helped someone!

Regards

查看更多
小情绪 Triste *
3楼-- · 2020-07-06 07:43

I had an error with the code above, so I tweaked mine a bit:

active admin init:

  config.clear_javascripts!
  config.register_javascript 'admin/active_admin.js'

  current_javascripts = config.javascripts.clone
  config.clear_javascripts!
  config.register_javascript 'application.js'
  current_javascripts.each{ |j| config.register_javascript j }

active admin js

  //= require active_admin/base

That's it!

查看更多
老娘就宠你
4楼-- · 2020-07-06 07:48

If you look at the activeadmin base manifest file you'll see where the additional jquery load is called. The last call in the base manifest is to the activeadmin application manifest. Therefore there is an easy way to bypass the unwanted additional jquery load.

Change this line in you application's /app/assets/javascripts/active_admin.js:

//= require active_admin/base

To

//= require active_admin/application

That way active admin's javascript code will be loaded without reloading jquery.

Within the /admin space, active admin loads active_admin.js without loading application.js, so you need to load application.js there too. To work, you need to make active admin load application.js before active_admin.js. Add this to config/initializers/active_admin.rb:

current_javascripts = config.javascripts.clone
config.clear_javascripts! 
config.register_javascript 'application.js'
current_javascripts.reverse.each{|j| config.register_javascript j}

However, note that for this to work seamlessly, you may need all these declarations in your app's application.js manifest:

//= require jquery
//= require jquery-ui
//= require jquery_ujs

Also as application.js is being loaded within active admin, you need to manage any namespace conflicts yourself.

查看更多
登录 后发表回答