diff --git a/Gemfile b/Gemfile index e19ab8d..eceefaa 100644 --- a/Gemfile +++ b/Gemfile @@ -66,3 +66,4 @@ group :test do end gem "tailwindcss-rails", "~> 4.4" +gem "faker", "~> 3.8", group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 4f6b1a9..0d25d35 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,6 +114,8 @@ GEM erubi (1.13.1) et-orbi (1.4.0) tzinfo + faker (3.8.0) + i18n (>= 1.8.11, < 2) ffi (1.17.4-aarch64-linux-gnu) ffi (1.17.4-aarch64-linux-musl) ffi (1.17.4-arm-linux-gnu) @@ -405,6 +407,7 @@ DEPENDENCIES bundler-audit capybara debug + faker (~> 3.8) image_processing (~> 1.2) importmap-rails jbuilder @@ -461,6 +464,7 @@ CHECKSUMS erb (6.0.3) sha256=e43685a8a0a0ea6a924871b2162e8953ef73147ce46b75b36d1f6774fd286e91 erubi (1.13.1) sha256=a082103b0885dbc5ecf1172fede897f9ebdb745a4b97a5e8dc63953db1ee4ad9 et-orbi (1.4.0) sha256=6c7e3c90779821f9e3b324c5e96fda9767f72995d6ae435b96678a4f3e2de8bc + faker (3.8.0) sha256=c147b308df73a90f27a4fc84f18d4c22ef0ad9c2a64b2b61c86fd0ca71753efc ffi (1.17.4-aarch64-linux-gnu) sha256=b208f06f91ffd8f5e1193da3cae3d2ccfc27fc36fba577baf698d26d91c080df ffi (1.17.4-aarch64-linux-musl) sha256=9286b7a615f2676245283aef0a0a3b475ae3aae2bb5448baace630bb77b91f39 ffi (1.17.4-arm-linux-gnu) sha256=d6dbddf7cb77bf955411af5f187a65b8cd378cb003c15c05697f5feee1cb1564 diff --git a/app/controllers/students_controller.rb b/app/controllers/students_controller.rb new file mode 100644 index 0000000..9ce0047 --- /dev/null +++ b/app/controllers/students_controller.rb @@ -0,0 +1,75 @@ +class StudentsController < ApplicationController + before_action :set_school, only: %i[ index new create ] + before_action :set_student, only: %i[ show edit update destroy ] + + # GET /students or /students.json + def index + @students = @school.students.all + end + + # GET /students/1 or /students/1.json + def show + end + + # GET /students/new + def new + @student = @school.students.build + end + + # GET /students/1/edit + def edit + end + + # POST /students or /students.json + def create + @student = @school.students.build(student_params) + + respond_to do |format| + if @student.save + format.html { redirect_to @student, notice: "Student was successfully created." } + format.json { render :show, status: :created, location: @student } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @student.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /students/1 or /students/1.json + def update + respond_to do |format| + if @student.update(student_params) + format.html { redirect_to @student, notice: "Student was successfully updated.", status: :see_other } + format.json { render :show, status: :ok, location: @student } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @student.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /students/1 or /students/1.json + def destroy + @student.destroy! + + respond_to do |format| + format.html { redirect_to school_students_path(@student.school_id), notice: "Student was successfully destroyed.", status: :see_other } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_student + @student = Student.find(params.expect(:id)) + end + + def set_school + @school = School.find(params.expect(:school_id)) + end + + # Only allow a list of trusted parameters through. + def student_params + params.expect(student: [ :first_name, :last_name, :email, :grade_level, :gender ]) + end +end diff --git a/app/models/school.rb b/app/models/school.rb index ea2efb1..e05b6b4 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -1,3 +1,4 @@ class School < ApplicationRecord + has_many :students, dependent: :destroy validates :name, presence: true end diff --git a/app/models/student.rb b/app/models/student.rb new file mode 100644 index 0000000..031257f --- /dev/null +++ b/app/models/student.rb @@ -0,0 +1,7 @@ +class Student < ApplicationRecord + belongs_to :school + + enum :gender, %i[male female other].index_by(&:itself) + + validates :first_name, :last_name, :grade_level, presence: true +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2ebff44..cb63b8d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -24,8 +24,9 @@
-