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 <h1>Listing rate_professors</h1>
<table> <tr> <th>Lname</th> <th>Helpfulness</th> <th>Explanation</th> <th>Technologyused</th> <th>Exams</th> <th>Homeworksorassignments</th> <th>Class</th> <th>Section</th> <th>Comments</th> <th></th> <th></th> <th></th> </tr>
<% @professor.rate_professors.each do |rate_professor| %> <tr> <td><%= professor.lname %></td> <td><%= rate_professor.Helpfulness %></td> <td><%= rate_professor.Explanation %></td> <td><%= rate_professor.TechnologyUsed %></td> <td><%= rate_professor.Exams %></td> <td><%= rate_professor.HomeworksOrAssignments %></td> <td><%= rate_professor.Class %></td> <td><%= rate_professor.Section %></td> <td><%= rate_professor.comments %></td> </tr> <% end %> </table>
<br />
<%= link_to 'New Rate professor', new_rate_professor_path %>