Teach Me To Code » Screencasts (iPhone/iPod)

Building a Star-Rating System in Ruby on Rails with jQuery


Listen Later

Specification
Clicking a star rating turns on the stars to the left of the star I clicked.
Clicking a star submits the star rating.
When I refresh the page, the star ratings should be persistent.
We’ll be using Rails’ functions including:
form_for
hidden_field
Rails Helpers
We’ll be using jQuery functions including:
click
each
ajax
<% form_id = "movie_#{movie.id}_rating" %>
<%= form_for movie.ratings.last || movie.ratings.build, :html => {:id => form_id , :class => "star_rating_form"} do |f| %>
<%= f.hidden_field :movie_id %>
<%= f.hidden_field :stars, :id => form_id + "_stars" %>
<% end %>
<% (1..5).each do |i| %>
  • <% end %>
    var set_stars = function(form_id, stars) {
    for(i=1; i <= 5; i++){
    if(i <= stars){
    $('#' + form_id + '_' + i).addClass("on");
    } else {
    $('#' + form_id + '_' + i).removeClass("on");
    }
    }
    }
    $(function() {
    $('.rating_star').click(function() {
    var star = $(this);
    var form_id = star.attr("data-form-id");
    var stars = star.attr("data-stars");
    $('#' + form_id + '_stars').val(stars);
    $.ajax({
    type: "post",
    url: $('#' + form_id).attr('action'),
    data: $('#' + form_id).serialize()
    })
    });
    $('.star_rating_form').each(function() {
    var form_id = $(this).attr('id');
    set_stars(form_id, $('#' + form_id + '_stars').val());
    });
    });
    ...more
    View all episodesView all episodes
    Download on the App Store

    Teach Me To Code » Screencasts (iPhone/iPod)By Charles Max Wood

    • 4.3
    • 4.3
    • 4.3
    • 4.3
    • 4.3

    4.3

    3 ratings


    More shows like Teach Me To Code » Screencasts (iPhone/iPod)

    View all
    JavaScript Jabber by Charles M Wood

    JavaScript Jabber

    235 Listeners

    iPhreaks by Charles M Wood

    iPhreaks

    17 Listeners

    Ruby Rogues by Charles M Wood

    Ruby Rogues

    45 Listeners

    The Freelancers' Show by Charles M Wood

    The Freelancers' Show

    23 Listeners

    Adventures in Angular by Charles M Wood

    Adventures in Angular

    33 Listeners

    React Native Radio by Jamon Holmgren, Robin Heinze, Mazen Chami

    React Native Radio

    57 Listeners

    My JavaScript Story by Charles M Wood

    My JavaScript Story

    4 Listeners

    JavaScript Jabber by Charles M Wood

    JavaScript Jabber

    61 Listeners

    Ruby Rogues by Charles M Wood

    Ruby Rogues

    21 Listeners

    Adventures in Angular by Charles M Wood

    Adventures in Angular

    15 Listeners