我有两个控制器: students_controller.rb teachers_controller.rb
这两个控制器都是用脚手架生成的。当我启动服务器时,学生的视图/学生/新加载得很好但是/ teachers / new给了我一些错误:
错误:
TeachersController#new
中的NoMethodError未定义的方法`attribute_method_matcher'为nil:NilClass Rails.root:/ Users / pdahal / RubyOnRails / apps / myapplication
Application Trace app / controllers / teachers_controller.rb:27:in
new' app/controllers/teachers_controller.rb:27:in
new'
teachers_controller.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
students_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
new.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
students_controller.rb具有完全相同的代码,工作正常。请提前帮助,谢谢。
答案 0 :(得分: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.
谢谢!