1

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?

2
  • I had the same problem yesterday. And it turned out that i had to put = require ./timepicker above everything else. Try that. Commented Mar 4, 2013 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. Commented Mar 4, 2013 at 9:40

1 Answer 1

2

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.
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.