2015-11-17 41 views
-1

我试图创建对特定教师的评分。我已经添加了学校ID到我的评级表中,但仍然得到错误:没有路线匹配{:action =>“show”,:controller =>“teachers”,:id =>“teacher_id”,:school_id => nil}缺少必需的键:[:school_id]

No route matches {:action=>"show", :controller=>"teachers", :id=>"teacher_id", :school_id=>nil} missing required keys: [:school_id]

redirect_to school_teacher_path(params[:school_id], [:teacher_id])

这里是我的路由文件的routes.rb:

Rails.application.routes.draw do 
    resources :schools do 
    resources :teachers 
    end 

    resources :teachers do 
    resources :ratings 
    end 

ratings_controller.rb:

class RatingsController < ApplicationController 
    def new 
    get_teacher 
    @rating = @teacher.ratings.build 
    end 

    def create 
    get_teacher 
    @rating = @teacher.ratings.build(rating_params) 
    if @rating.save 
     redirect_to school_teacher_path(params[:school_id], [:teacher_id]) 
    else 
     render 'new' 
    end 
    end 

    def get_teacher 
    @teacher = Teacher.find(params[:teacher_id]) 
    end 

    private 

    def rating_params 
     params.require(:rating).permit(:easiness, :helpful, :clarity, :comment, 
     :teacher_id, :school_id) 
    end 
end 

ratings/new.html.erb:

<h1>Teacher Rating</h1> <%= form_for([@teacher, @rating]) do |f| %> <p> 
    <%= f.label :clarity %> 
    <%= f.text_field :clarity %> </p> 

    <p> 
    <%= f.label :easiness %> 
    <%= f.text_field :easiness %> </p> 

    <p> 
    <%= f.label :helpfulness %> 
    <%= f.text_field :helpfulness %> </p> 

    <p> 
    <%= f.label :comment %> 
    <br> 
    <%= f.text_area :comment %> </p> 

    <p> 
    <%= f.submit %> </p> <% end %> 

rating.rb:

class Rating < ActiveRecord::Base 
    belongs_to :teacher, dependent: :destroy 
end 

teacher.rb:

class Teacher < ActiveRecord::Base 
    belongs_to :school 
    has_many :ratings 

    def name 
    "#{firstName} #{middleName} #{lastName}" 
    end 

    def to_s 
    name 
    end 
end 

school.rb:

class School < ActiveRecord::Base 
    has_many :teachers, dependent: :destroy 
    validates :name, presence: true, 
        length: { minimum: 5 } 
end 
+2

请检查您的PARAMS在创建行动阉PARAMS [:学校ID]是来还是不来,并请修改redirect_to的school_teacher_path(PARAMS [:学校ID] params [:teacher_id]) – railslearner

+0

谢谢你的帮助,raje先生。我编辑了该行,但现在出现错误:{:action =>“show”,:controller =>“teachers”,:id =>“8”,:school_id => nil}缺少必需的键:[: school_id] – NeverGiveUp1989

回答

1

在这一行:

redirect_to school_teacher_path(params[:school_id], [:teacher_id]) 

您传递params[:school_id]变量,然后是包含sym的数组bol :teacher_id

我怀疑你真的打算用这样的:

redirect_to school_teacher_path(params[:school_id], params[:teacher_id]) 
+0

谢谢你的帮助,乔恩先生。但是,现在我得到错误: 没有路线匹配{:action =>“show”,:controller =>“teachers”,:id =>“8”,:school_id => nil}缺少必需的键:[:school_id ] – NeverGiveUp1989

+1

那么你的params变量中没有'school_id'。在'create'动作的某个地方,您需要找到需要传递给路由帮助程序的'school_id',或者您需要修复提交您的创建请求的任何内容,以便将school_id与其余数据一起发送。 – Jon

+0

我将此行添加到创建操作'@school = School.find(params [:school_id])'中。然后,我得到错误:无法找到学校'ID'= – NeverGiveUp1989

相关问题