滑轨的form_for NoMethodError [英] Rails form_for NoMethodError
问题描述
我想生成Rails应用程序中使用雇员再培训局的形式。我不断地得到NoMethodError线路#我.html.erb文件3。下面是迁移,控制器,所述模型,和所述.html.erb
I am trying to generate a form using erb in a rails application. I am continually getting the NoMethodError for line #3 of my .html.erb file. Below is the migration, the controller, the model, and the .html.erb
该错误是未定义的方法`class_projects_path'
The error is "undefined method `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.html.erb:
<h1>New Class Project</h1>
<%= form_for @class_project do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :summary %>
<%= f.text_field :summary %>
<%= f.label :github %>
<%= f.text_field :github %>
<%= f.label :other_url %>
<%= f.text_field :other_url %>
<% end %>
路线的好办法:
Route for good measure:
get 'new_project' => 'class_projects#new', :as => 'new'
感谢您的帮助,inb4学习code要点,使用搜索功能要点,等等。
Thanks for any help, inb4 learn to code nub, use the search function nub, etcetera.
推荐答案
您需要使用以下命令:
#config/routes.rb
resources :class_projects
这意味着你就可以使用以下命令:
This means you'll be able to use the following:
#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
对象路线的完整的恭维。 Rails的运行 足智多谋
(面向对象)的路由系统,这意味着,如果你调用资源
指令,你将有一个完整的CRUD路由结构提供了:
The problem you have is that you have not declared a full compliment of routes for your class_projects
objects. Rails runs a resourceful
(object orientated) routing system, meaning that if you invoke the resources
directive, you'll be provided with a full CRUD routing structure:
这意味着,如果你使用的辅助,例如的form_for
(其中建立了基于您提供的对象上的路线),你的有无的到有设置路由的这个CRUD恭维。
This means that if you use a helper such as form_for
(which builds routes based on your provided object), you'll have to have this CRUD compliment of routes set up.
定义你的路由是上面的文件会给你打电话的的form_for
帮助逍遥法外(没有额外的参数)的能力。
Defining your routes file as above will give you the ability to call the form_for
helper with impunity (and no extra arguments)
这篇关于滑轨的form_for NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!