我正在尝试为特定教师创建评分。我已将school_id添加到我的评级表中,但仍然收到错误:
没有路线匹配{:action =>" show",:controller =>"老师", :id =>" teacher_id",:school_id => nil}缺少必需的密钥: [:学校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
评价/ 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
答案 0 :(得分:1)
在这一行:
redirect_to school_teacher_path(params[:school_id], [:teacher_id])
您正在传递params[:school_id]
变量,然后传递包含符号:teacher_id
的数组。
我怀疑你真的打算用这样的东西:
redirect_to school_teacher_path(params[:school_id], params[:teacher_id])