0
  $(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?

views/orders/_form.html.erb

<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 %>

application.html.erb has

<%= javascript_include_tag 'application' %>

4
  • 1
    I think you need to state your problem better in order to get any valuable help. Be more specific about what part that's not working. Commented May 29, 2014 at 21:50
  • See if this helps. You may need to enclose your javascript in jQuery function. Commented May 29, 2014 at 21:57
  • 1
    @gernberg I tried to clarify my question, please check above Commented May 29, 2014 at 22:19
  • 1
    @kiddorails that refers to coffee script, I am not using any coffee script Commented May 29, 2014 at 22:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I was missing require_self, oboi. Thanks
If I put the js code in assets/javascript/orders.js and add require orders in application.js would the code work? would that be the right way to do this sort of stuff?
ah it worked putting it in orders.js if I add require orders under the require self, perfect

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.