Reputation: 485
$(document).ready(function() {
$('#client-select2').select2();
});
If i place the code above to application.js, it does not respond but if I place it in the _form.html.erb file, it does. Anyone know why that is?
<script type="text/javascript">
$(document).ready(function() {
$('#client-select2').select2();
});
</script>
<%= simple_form_for(@order) do |f| %>
<%= f.error_notification %>
<%= f.input :code %>
<%= f.association :client, collection: Client.all, label_method: :name, value_method: :id, prompt: "Choose a Client", required: true, input_html: { id: 'client-select2' } %>
<%= f.submit %>
<% end %>
<%= javascript_include_tag 'application' %>
Upvotes: 0
Views: 668
Reputation: 2617
The reason could be that select2 and/or jQuery is included after your application.js,
If for instance, if the order is:
//=require_self
//=require select2
//=require jquery
The jquery/select2 functions will not be available in application.js since application.js is included before.
Upvotes: 2