StudentsController中的ActiveRecord :: RecordNotFound #new找不到'id'= all的学生

时间:2015-11-17 09:52:57

标签: ruby-on-rails

我刚开始使用ruby 试图创建一个在线表单 它开始显示上述错误 任何人都可以帮我这个 我的代码students_controller.rb

 class StudentsController < ApplicationController
 def new
@student = Student.new
@students = Student.find(:all)
end

def create
@student = Student.new(student_params)
if @student.save
    redirect_to new_student_path
end
end
def student_params
allow = [:firstname, :lastname]
params.require(:student).permit(allow)

end

end

我的new.html代码

Enter new student information 
<hr>
<%= form_for @student do |f| -%>
 Firstname: <%= f.text_field :firstname %><br /><br />
 Lastname:  <%= f.text_field :lastname %><br /><br />
<%= f.submit %>
<% end -%>
<hr>
Display all students' information
<% if !@students.blank? %>
<% for item in @students %>
   <%= item.firstname %> <%= item.lastname %> <br />
<% end %>
<% else %>
<% end %>

我的路线

Rails.application.routes.draw do
resources :students

1 个答案:

答案 0 :(得分:2)

使用

@students = Student.all

而不是

@students = Student.find(:all)