Joshua
Joshua

Reputation: 805

Using Jquery within rails

I am trying to use some Jquery witihin a rails application. This is my first time using Jquery in rails and I cannot seem to get it to work. I am using rails4 and this is what my code looks like

I have a pages controller with an index action

index action

def index
  respond_to do |format|
   format.html
   format.js
  end
end

pages.js

$(document).ready(function(){
$('.button').click(function(){
    $(this).fadeOut('slow);
  });
});

index.html.erb

 <button class="button">Clickme</button>

However I get no response, am I missing something here ?

Upvotes: 0

Views: 126

Answers (2)

kalkov
kalkov

Reputation: 145

Rails4 uses turbolinks.

So in coffescript your code should be:

pages.js.coffee

ready = ->
  $('.button').click -> 
    $(this).fadeOut('slow')


$(document).ready(ready)
$(window).bind('page:change', ready)

Upvotes: 1

tobinjim
tobinjim

Reputation: 1852

Is your controller "pages"? Where is "pages.js" located?

If "pages" is the controller, and "pages.js" is in /app/assets/javascripts/ then I am of no help. But in my test app, I placed your javascript in the .js file for the controller, in /app/assets/javascripts/ and the button fades out like you expected.

Upvotes: 0

Related Questions