How do I get the jquery-cookie plugin to work with Rails 5?

1.3k views Asked by At

I want to use Rails 5.0.1 and I also need the jQuery cookie plugin in my application. I had included this in my Gemfile

gem 'jquery-cookie-rails'

but since changing my Rails dependency to 5.0.1, I now get this error when I run bundle install ...

localhost:myproject davea$ bundle install
Your Gemfile lists the gem pg (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (= 5.0.1)

  In Gemfile:
    coffee-rails was resolved to 4.2.1, which depends on
      railties (< 5.2.x, >= 4.0.0)

    jquery-cookie-rails was resolved to 0.1.1, which depends on
      railties (< 5.0, >= 3.2.0)

    jquery-rails was resolved to 4.2.1, which depends on
      railties (>= 4.2.0)

    jquery-turbolinks was resolved to 2.1.0, which depends on
      railties (>= 3.1.0)

    jquery-ui-rails was resolved to 6.0.1, which depends on
      railties (>= 3.2.16)

    rails (~> 5.0.1) was resolved to 5.0.1, which depends on
      railties (= 5.0.1)

    sass-rails was resolved to 5.0.6, which depends on
      railties (< 6, >= 4.0.0)

    web-console was resolved to 3.4.0, which depends on
      railties (>= 5.0)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

How can I have my cake and eat it too? I want to use the latest version of Rails and I also want to use jQuery-cookie. How can I do that?

2

There are 2 answers

0
Mayur Shah On

Try to install updated Gem Using gem install jquery-cookie-rails --version 1.3.1.1, Find the latest version from here

Or You can do like this:

must put the file jquery.cookie.js in the folder vendor/assets/javascripts/, then at the file app/assets/javascripts/application.js it must be called with:

//= require jquery.cookie

before

//= require_tree .

or any other require that needs jquery.cookie

You don't need a script tag.