I'm partially new to RoR and fairly new to jQuery. Currently, I have a working RoR site as a learning platform. I want to include some jQuery basic features to expand my learning (.mouseenter(), .hover(), .fadeIn() etc).
Let me set the scene with some code (I've snipped parts to keep it short):
$ ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
$ rails -v
Rails 3.2.8
Gemfile:
gem 'jquery-rails'
config/routes.rb:
root to: 'static_pages#home'
app/controllers/static_pages_controller.rb:
def home
@Presents = Present.all.reverse.take(20)
end
app/views/layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>List</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<div class="container-narrow">
<%= render 'layouts/header' %>
<%= yield %>
</div>
</body>
</html>
app/assets/javascripts/application.js:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
app/views/static_pages/home.html.erb:
<div class="jumbotron">
<h1>Heading</h1>
</div>
<%= render 'shared/list' %>
app/views/shared/_list.html.erb:
<% if @Presents.any? %>
<%= render partial: 'shared/list_item', collection: @Presents %>
<% end %>
app/views/shared/_list_item.html.erb:
<div id="present">
<ul id="<%= list_item.id %>">
<span class="content">
Some content here
</span>
</div>
Ideally, I want my jQuery to effect the <div>
with id="present"
. Here is my test jQuery:
$(document).ready(function(){
$('#present').mouseenter(function(){
$(this).fadeIn('fast',1);
}
$('#present').mouseleave(function(){
$(this).fadeIn('fast',0.5);
}
});
Currently, I have the above stored app/views/static_pages/home.js.erb
and nothing happens. Is this an appropriate location? Or should I shift it to the app/views/shared/
directory?
From my rendered site page - is there a way to check if my jQuery is being included and executed? I feel my current blocking point is the location of my home.js.erb.
Edit: errors detected in my jQuery - corrected below:
jQuery:
$(document).ready(function(){
$('#present').mouseenter(function(){
$(this).fadeTo('fast',1);
});
$('#present').mouseleave(function(){
$(this).fadeTo('fast',0.5);
});
});
and correct use of fadeTo
. fadeIn
doesn't accept a second argument for opacity as I was trying.