所以我正在尝试设置一个黄瓜测试,访问将访问课程索引页面,然后点击一个课程去特定课程的显示页面。然而,最终发生的事情是,一切都按预期进行(在课程控制器中点击show动作,显示正确的@student对象,一旦路径被激活它应该重定向到),但它实际上不会这样做。它会命中课程控制器,并导致No route matches {:action=>"show", :controller=>"courses", :id=>nil} missing required keys: [:id] (ActionView::Template::Error)
错误消息。
这是我的设置:
When(/^I select a student$/) do
click_on "#{@student.first_name} #{@student.last_name}"
end
Then(/^I should see their information$/) do
expect(page).to have_content(@student.first_name)
end
Scenario: See Individual Student Page
Given I am logged in as a flatiron employee
When I visit the students page
And I select a student
Then I should see their information
class CoursesController < ApplicationController
def index
@courses = Course.all
end
def new
@course = Course.new
end
def create
@course = Course.new(course_params)
if @course.save
flash[:notice] = "Course has successfully been saved."
redirect_to course_path(@course)
else
flash[:notice] = "Error! Course was not successfully saved."
redirect to new_course_path
end
end
def show
@course = Course.find(params[:id])
end
private
def course_params
params.require(:course).permit(:name)
end
end
class StudentsController < ApplicationController
before_filter :set_student, only: [:show, :edit, :update, :destroy]
def index
@students = Student.all
end
def show
end
def new
@student = Student.new
end
def edit
end
def create
@student = Student.new(student_params)
if @student.save
flash[:notice] = "Student form successfully submitted."
redirect_to students_path
else
flash[:error] = "Unable to process your request."
render :new
end
end
def update
Student.update(@student, student_params)
redirect_to student_path(@student)
end
def destroy
@student.destroy
redirect_to courses_path
end
private
def set_student
@student = Student.find(params[:id])
end
def student_params
params.require(:student).permit(removed_attributes)
end
end
Registrar::Application.routes.draw do
resources :courses do
resources :students, controller: :courses_students, only: [:new, :create, :show]
end
resources :students
devise_for :users, :path_names => { sign_in: 'login', sign_out: 'logout' }, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks",
:sessions => "sessions"
}
root 'pages#landing'
end
<br><br>
<%= @student.first_name %>
<%= @student.last_name %><br>
<br>
<%= link_to "Back to Course", course_path(@student.course) %>
<%= link_to "Students", students_path %>
<br>
<br><br>
<% @students.each do |student| %>
<%= link_to "#{student.first_name} #{student.last_name}", student_path(student) %><br>
<% end %>
<br>
<%= link_to "Landing Page", root_path %>
非常感谢任何帮助!