in my search for .html.haml, I want the user to be able to click on the teacher’s name and this will lead them to the teacher’s showing page. However, it also requires me to have school_id, because the teacher belongs to the school, and the school has many teachers. Now, I wonder if there is a way for me to school_id the way without breaking the application. The error the rails are throwing at me now:
There are no route matches {: action => "show" ,: controller => "teachers" ,: id => "1" ,: school_id => nil} the required keys are missing: [: school_id]
Here are my files:
search.html.haml
.text-center
/ No search results announcement/notification
- if @teachers.blank?
%h2 Xin lỗi, hệ thống chúng tôi không có thông tin về giảng viên mà bạn muốn tìm.
- else
- @teachers.each do |teacher|
%h2= link_to teacher.fullName, school_teacher_path(@school, teacher)
%em
Khoa
teachers_controller.rb
class TeachersController < ApplicationController
before_action :find_school, except: [:welcome, :search]
before_action :find_teacher, only: [:show, :edit, :update, :destroy]
def welcome
end
def show
end
def search
if params[:search].present?
@teachers = Teacher.search(params[:search], fields: [:fullName])
else
@teachers = nil
end
end
def new
@teacher = @school.teachers.build
end
def create
@teacher = @school.teachers.create(teacher_params)
@teacher.save
redirect_to(@school)
end
def edit
end
def update
@teacher.update(teacher_params)
redirect_to(@school)
end
private
def find_school
@school = School.find(params[:school_id])
end
def find_teacher
@teacher = Teacher.find(params[:id])
end
def teacher_params
params.require(:teacher).permit(:firstName, :lastName, :middleName, :department, :school_id, :fullName)
end
end
teacher.rb
class Teacher < ActiveRecord::Base
belongs_to :school
has_many :ratings
searchkick
def name
"#{lastName} #{middleName} #{firstName}"
end
def to_s
name
end
end
school.rb
class School < ActiveRecord::Base
has_many :teachers, dependent: :destroy
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :schools do
resources :teachers do
collection do
get 'search'
end
end
end
resources :teachers do
collection do
get 'search'
end
resources :ratings
end
root 'teachers#welcome'
end