Rails form_for NoMethodError

我试图在rails应用程序中使用erb生成表单。 我不断得到我的.html.erb文件第3行的NoMethodError。 下面是迁移,控制器,模型和.html.erb

错误是“未定义的方法`class_projects_path’”

移民:

class CreateClassProjects < ActiveRecord::Migration def change create_table :class_projects do |t| t.string :name t.text :description t.text :summary t.text :github t.text :other_url t.timestamps end end end 

模型:

 class ClassProject < ActiveRecord::Base attr_accessible :description, :github, :name, :other_url, :summary end 

控制器:

 class ClassProjectsController < ApplicationController def new @class_project = ClassProject.new end end 

new.html.erb:

 

New Class Project

良好措施的路线:

 get 'new_project' => 'class_projects#new', :as => 'new' 

感谢您的帮助,inb4学习代码nub,使用搜索functionnub,等等。

您需要使用以下内容:

 #config/routes.rb resources :class_projects 

这意味着您将能够使用以下内容:

 #app/controllers/class_projects_controller.rb class ClassProjectsController < ApplicationController def new @class_project = ClassProject.new end end #app/views/class_projects/new.html.erb <%= form_for @class_project do |f| %> ... <% end %> 

路线

您遇到的问题是您没有为class_projects对象声明完整的路由class_projects 。 Rails运行一个resourceful ( 面向对象 )的路由系统,这意味着如果你调用resources指令,你将获得一个完整的CRUD路由结构:

在此处输入图像描述

这意味着如果您使用诸如form_for的帮助程序(根据您提供的对象构建路径), 必须设置此CRUD补充路由。

如上定义您的路由文件将使您能够调用form_for帮助程序而不受惩罚(并且没有额外的参数)

您没有定义资源路由,因此您的表单需要额外的配置

 <%= form_for @class_project, url: new_path do |f| %