0

我有两个控制器:students_controller.rb Teachers_controller.rb

这两个控制器都是用脚手架生成的。当我启动服务器时,学生的视图 /students/new 加载得很好,但是 /teachers/new 给了我一些错误:

错误:

TeachersController 中的 NoMethodError#new

nil 的未定义方法“attribute_method_matcher”:NilClass Rails.root:/Users/pdahal/RubyOnRails/apps/myapplication

应用程序跟踪 app/controllers/teachers_controller.rb:27:in new' app/controllers/teachers_controller.rb:27:innew'

教师控制器.rb:

# GET /teachers/new
# GET /teachers/new.json
  def new
    @teacher = Teacher.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @teacher }
    end

student_controller.rb

  # GET /students/new
  # GET /students/new.json
  def new
    @student = Student.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @student }
    end
  end

新的.html.erb:

<%- model_class = Teacher -%>
<div class="page-header">
  <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>

数据库/schema.rb

ActiveRecord::Schema.define(:version => 20131113051714) do

  create_table "students", :force => true do |t|
    t.string   "name"
    t.integer  "age"
    t.string   "sex"
    t.string   "email"
    t.string   "major"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "teachers", :force => true do |t|
    t.string   "name"
    t.integer  "age"
    t.string   "email"
    t.string   "sex"
    t.string   "class"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

student_controller.rb 具有完全相同的代码,并且工作正常。请帮助,提前谢谢。

4

1 回答 1

0

错误是因为我在class上使用了保留字:

create_table "teachers", :force => true do |t|
    t.string   "name"
    t.integer  "age"
    t.string   "email"
    t.string   "sex"
    t.string   "class"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

我删除了那个脚手架并重新生成了新的:

rails g teacher name age:integer email sex course 
#replaced class with course, now its working great.

谢谢!

于 2013-11-15T22:47:24.250 回答