Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got this problem writing the website with Ruby on Rails.

bundle show
* jquery-ui-rails (4.0.1)

In app/assets/javascripts/application.js

//= require jquery.ui.datepicker

In app/assets/stylesheets/application.css

*= require jquery.ui.datepicker

In app/views/layouts/application.html.erb

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>

And then in somepage.html.erb, I got

<script type="text/javascript">
$(function(){
    $("#startdate").datepicker();
    $("#enddate").datepicker();
});
</script>

When running it, Chrome says that

Uncaught TypeError: Object [object Object] has no method 'datepicker'

I suppose that resource not being referred properly is the cause because the problem could be fixed by adding the follows into app/views/layouts/application.html.erb

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />

These could be found at http://jqueryui.com/datepicker/. I saw demos having working datepickers without these codes. But I do not understand why it is not working in mine. Anyone got any suggestions? like something to add other than those I mentioned above?

share|improve this question
I had the same problem yesterday. And it turned out that i had to put = require ./timepicker above everything else. Try that. – Vezu Mar 4 at 9:15
You don't have to include any other js but only one: <%= javascript_include_tag "application" %> and make sure your assets folder contains jquery js within javascripts. – Saurabh Jain Mar 4 at 9:40

1 Answer

up vote 0 down vote accepted

It probably because of either of the following:

1) You are including multiple javascript files in your application.html.erb which is leading to havoc when put altogether.

2) You are using some other javascript file that also using $ just like jquery is using. Having two same symbols is the possible cause of getting no method 'datepicker' for the jquery.

The alternative is to replace all the occurences of $ with jQuery

OR

Just wrap your jquery code insode a block like the following:

jQuery(function($){
    //all jQuery code which uses $ should be here.
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.