Search code examples
jqueryruby-on-railstwitter-bootstrapruby-on-rails-3.2twitter-bootstrap-rails

Rails 3.2 and jQuery function not working in production


I'm having a bit of trouble getting my javascript function that is compiled by the Asset Pipeline to work in production. It is working fine in development.

Javascript

# app/assets/javascript/subscribe.js

$(function() {
  $('#fat-btn').click(function() {
    var btn = $(this)
    btn.button('loading')
    setTimeout(function () {
      btn.button('reset')
    }, 2000)
  });
});

My view

# app/assets/subscribe.html.erb

<div class="subscribe">
  <%= form_tag subscribe_path, class: "form-inline" do %>
      <%= text_field_tag :email, nil, :type=>"email", :placeholder => 'Your email address' %>
      <%= button_tag "Go", id: "fat-btn", class: "btn", data: { "loading-text" => "loading...", "toggle" => "button"} %>
  <% end %>
</div>

My Application Javascript

# app/assets/javascript/application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

Any help is much appreciated it. Thank you


Solution

  • This happened to me before and in my case I was calling multiple instances of JQuery. So:

    • Make sure you're not calling multiple instances of JQuery.

    • If you are using coffeescript and have a syntax error (I had extra spaces in my case) in development all can go well but in producion it will not. Again, the console's log when you precompile should point out any issues if there are any.

    • See if there are any errors in Firebug when you are in development
      and production
      .

    • If all that fails you can try to clean and precompile your assets
      again.