ruby on rails - How can I add school_id to my path? -
in search.html.haml, want user have ability click on teacher name , lead them teacher show page. however, requires me have school_id because teacher belongs school, , school has many teachers. now, wondering if there way me school_id path without breaking application. error rails throwing @ me is:
no route matches {:action=>"show", :controller=>"teachers", :id=>"1", :school_id=>nil} missing required keys: [:school_id]
here 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 |teacher| %h2= link_to teacher.fullname, school_teacher_path(@school, teacher) #note %em khoa #{teacher.department}, trường #{teacher.school.name}
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 # searchkick end
routes.rb:
rails.application.routes.draw devise_for :users resources :schools # collection # 'search' # end resources :teachers collection 'search' end end end resources :teachers collection 'search' end resources :ratings end root 'teachers#welcome' end
school_teacher_path
called @school
nil since find_school
not used search
. want replace @school
teacher.school
: school_teacher_path(teacher.school, teacher)
.
Comments
Post a Comment