Timothé Gauguet
Timothé Gauguet

Reputation: 183

Error with jQuery and rails

I got a problem with jQuery.ui and Rails. I try to solve this error since 6 hours but I can't find a solution. This is especially annoying that it seems simple. Sure I am missing something.

When I add <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> to the head of my application.html.erb, it works but only if the input is placed inside application.html.erb :

<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

<form method="post">
   Date:<input type="text" id="datepicker" name="datepicker" />
</form>

If the form is placed in ANY other view, the date picker function will not works.

in application.js:

//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require jquery.turbolinks 
//= require turbolinks
//= require welcome
//= require subscriptions
//= require to_do_lists
//= require_tree .

$(function() {
    $( "#datepicker" ).datepicker();
});

in gemfile :

gem 'jquery-ui-rails', '~> 5.0', '>= 5.0.5'

in application.css :

/*
 *= require_self
 *= require jquery.ui.all
*/

I face two problems :

  1. First, jQuery-ui gem won't works
  2. Second, even if I call the script in the head of my app, it works only with a form in application.html.erb

EDIT with my Gemfile:

source 'https://rubygems.org'
source 'https://code.stripe.com' do
    gem 'stripe'
end

ruby "2.2.4"

gem 'rails', '4.2.3'

gem 'pg', '~> 0.15'

gem 'sass-rails', '~> 5.0'

gem "gibbon"

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.1.0'

gem 'aws-sdk', '< 2.0'

gem 'jbuilder', '~> 2.0'

gem 'friendly_id', '~> 5.0.0'

gem 'sdoc', '~> 0.4.0', group: :doc

gem 'rails_12factor'

gem 'meta-tags'

gem 'devise'

gem 'factory_girl_rails'

gem 'rails_serve_static_assets'

gem 'capybara'

gem 'kaminari'

gem 'figaro'

gem 'guard-rspec'

gem 'paperclip'

# jquery gems for rails
gem "jquery-rails"

gem 'jquery-ui-rails'

gem 'puma'

gem 'therubyracer'

gem 'paper_trail'

group :development, :test do

  gem 'byebug'
  gem 'stripe-ruby-mock'
  gem 'database_cleaner'
  gem 'rspec-rails'
  gem 'rails_best_practices'
  gem 'brakeman'
  gem 'quiet_assets'

end

group :development do

  gem 'web-console', '~> 2.0'

  gem 'spring'
end

Any idea ?

Upvotes: 1

Views: 1062

Answers (3)

Timoth&#233; Gauguet
Timoth&#233; Gauguet

Reputation: 183

I solved the problem, I just added a jquery script in my application.html.erb that was the cause of the problem... Thanks everyone !

Upvotes: 0

Akshay Borade
Akshay Borade

Reputation: 2472

I think you are not requiring plugin properly in application.js and application.css. Please try this and let me know if you still facing problem.

In your Gemfile, add:

gem 'jquery-ui-rails'

Require Everything

To require all jQuery UI modules, add the following to your application.js:

//= require jquery-ui

Also add the jQuery UI CSS to your application.css:

/* *= require jquery-ui */

Upvotes: 1

Quentin Boussard
Quentin Boussard

Reputation: 60

Just remove turbolinks on your project

I had the same problem.

Upvotes: 0

Related Questions