I was trying to make one to many relationships - between professors
controller to rate_professors controller and used scaffold. it’s
giving me this error-
NoMethodError in RateProfessorsController#index
undefined method rate_professors' for nil:NilClass app/controllers/rate_professors_controller.rb:7:in
index’
Request
Parameters:
{“professor_id”=>“1”}
here are the codes for two controllers and index page–
class ProfessorsController < ApplicationController
GET /professors
GET /professors.json
def index
@professors = Professor.all
@rate_professors=RateProfessor.order(:lname)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @professors }
end
end
GET /professors/1
GET /professors/1.json
def show
@professor = Professor.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @professor }
end
end
GET /professors/new
GET /professors/new.json
def new
@professor = Professor.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @professor }
end
end
GET /professors/1/edit
def edit
@professor = Professor.find(params[:id])
end
POST /professors
POST /professors.json
def create
@professor = Professor.new(params[:professor])
respond_to do |format|
if @professor.save
format.html { redirect_to @professor, notice: 'Professor was
successfully created.’ }
format.json { render json: @professor, status: :created,
location: @professor }
else
format.html { render action: “new” }
format.json { render json: @professor.errors,
status: :unprocessable_entity }
end
end
end
PUT /professors/1
PUT /professors/1.json
def update
@professor = Professor.find(params[:id])
respond_to do |format|
if @professor.update_attributes(params[:professor])
format.html { redirect_to @professor, notice: 'Professor was
successfully updated.’ }
format.json { head :no_content }
else
format.html { render action: “edit” }
format.json { render json: @professor.errors,
status: :unprocessable_entity }
end
end
end
DELETE /professors/1
DELETE /professors/1.json
def destroy
@professor = Professor.find(params[:id])
@professor.destroy
respond_to do |format|
format.html { redirect_to professors_url }
format.json { head :no_content }
end
end
end
For rate_professors controller:
class RateProfessorsController < ApplicationController
GET /rate_professors
GET /rate_professors.json
def index
@rate_professors = RateProfessor.all
professor = Professor.find(params[:professor_id])
@rate_professor =
@Professor.rate_professors.build([professor:professor])
@rate_professors
respond_to do |format|
format.html # index.html.erb
format.json { render json: @rate_professors }
end
end
GET /rate_professors/1
GET /rate_professors/1.json
def show
@rate_professor = RateProfessor.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @rate_professor }
end
end
GET /rate_professors/new
GET /rate_professors/new.json
def new
@professor = Professor.find(params[:id])
@rate_professor = RateProfessor.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @rate_professor }
end
end
GET /rate_professors/1/edit
def edit
@rate_professor = RateProfessor.find(params[:id])
end
POST /rate_professors
POST /rate_professors.json
def create
@professor = Professor.find(params[:id])
@rate_professor =
@Professor.rate_professors.build([professor:professor])
respond_to do |format|
if @rate_professor.save
format.html { redirect_to @rate_professor.professor, notice:
‘Rate professor was successfully created.’ }
format.json { render json: @rate_professor, status: :created,
location: @rate_professor }
else
format.html { render action: “new” }
format.json { render json: @rate_professor.errors,
status: :unprocessable_entity }
end
end
end
PUT /rate_professors/1
PUT /rate_professors/1.json
def update
@rate_professor = RateProfessor.find(params[:id])
respond_to do |format|
if @rate_professor.update_attributes(params[:rate_professor])
format.html { redirect_to @rate_professor, notice: 'Rate
professor was successfully updated.’ }
format.json { head :no_content }
else
format.html { render action: “edit” }
format.json { render json: @rate_professor.errors,
status: :unprocessable_entity }
end
end
end
DELETE /rate_professors/1
DELETE /rate_professors/1.json
def destroy
@rate_professor = RateProfessor.find(params[:id])
@rate_professor.destroy
respond_to do |format|
format.html { redirect_to rate_professors_url }
format.json { head :no_content }
end
end
end
index page
Listing rate_professors
Lname | Helpfulness | Explanation | Technologyused | Exams | Homeworksorassignments | Class | Section | Comments | |||
---|---|---|---|---|---|---|---|---|---|---|---|
<%= professor.lname %> | <%= rate_professor.Helpfulness %> | <%= rate_professor.Explanation %> | <%= rate_professor.TechnologyUsed %> | <%= rate_professor.Exams %> | <%= rate_professor.HomeworksOrAssignments %> | <%= rate_professor.Class %> | <%= rate_professor.Section %> | <%= rate_professor.comments %> |
<%= link_to ‘New Rate professor’, new_rate_professor_path %>