New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add students resource and major filter #1

Merged
merged 1 commit into from Mar 23, 2016
Jump to file or symbol
Failed to load files and symbols.
+110 −0
Diff settings

Always

Just for now

@@ -0,0 +1,11 @@
jQuery(function(){
var cookie_val = document.cookie.replace(/(?:(?:^|.*;\s*)major\s*\=\s*([^;]*).*$)|^.*$/, "$1");
$('select').val(cookie_val);
$('select').change(function(){
var major = $(this).val();
document.cookie = "major=" + major;
window.location.href = window.location.href.replace( /[\?#].*|$/, "?major=" + major );
})
});
@@ -0,0 +1,7 @@
// Place all the styles related to the students controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
table, td {
border: 1px solid black;
}
@@ -0,0 +1,7 @@
class StudentsController < ApplicationController
def index
choice = params[:major]
@students = choice.nil? || choice == "all majors" ? Student.all : Student.where(major: choice)
@majors = Student.majors
end
end
View
@@ -0,0 +1,6 @@
class Student < ApplicationRecord
def self.majors
records = Student.select(:major).distinct
records.map(&:major)
end
end
@@ -0,0 +1,23 @@
<h1>Students at University</h1>
<select id="major-select">
<option selected disabled>Filter by major:</option>
<% @majors.each do |major| %>
<option><%= major %></option>
<% end %>
<option>all majors</option>
</select>
<p>
<table>
<tr>
<td>Name</td>
<td>Major</td>
<td>GPS</td>
</tr>
<% @students.each do |student| %>
<tr>
<td> <%= student.name %> </td>
<td> <%= student.major %> </td>
<td> <%= student.gpa %> </td>
</tr>
<% end %>
</table>
View
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
root to: 'students#index'
end
@@ -0,0 +1,11 @@
class CreateStudents < ActiveRecord::Migration[5.0]
def change
create_table :students do |t|
t.string :name
t.string :major
t.string :gpa
t.timestamps
end
end
end
View
@@ -0,0 +1,24 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160305195953) do
create_table "students", force: :cascade do |t|
t.string "name"
t.string "major"
t.string "gpa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
View
@@ -0,0 +1,18 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
names = %w(Aaron Beth Cathy Darius Eliza Fiona Gertrude Heidi Lauren Mary Norie Opal Priscilla Queen Reba Sharon Tim Viola Yvonne Zelda)
majors = %w(English CS math music engineering informatics)
def gpa
rand(5).to_s
end
100.times do
Student.create(name: names.sample, major: majors.sample, gpa: gpa)
end
ProTip! Use n and p to navigate between commits in a pull request.